考虑以下结构
message Fly {
uint32 dtime = 1;
}
但是 HasField
函数不起作用:
>>> d.ListFields()[0][0].name
'dtime'
>>> d.ListFields()[0][0].full_name
'Fly.dtime'
>>>
>>> d.HasField('dtime')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/.../lib/python2.7/site-packages/google/protobuf/internal/python_message.py", line 825, in HasField
raise ValueError(error_msg % field_name)
ValueError: Protocol message has no non-repeated submessage field "dtime"
>>> d.HasField('Fly.dtime')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/.../lib/python2.7/site-packages/google/protobuf/internal/python_message.py", line 825, in HasField
raise ValueError(error_msg % field_name)
ValueError: Protocol message has no non-repeated submessage field "Fly.dtime"
HasField
需要哪些参数?
最佳答案
我认为您误解了 HasField()
的作用。它不会检查 protobuf 类型是否通过名称定义了特定字段。它的作用是检查给定消息 字段的名称是否为当前实例设置了该字段。
作为documentation请注意,为 proto3 中的非消息字段调用 HasField 将引发错误。
关于python - protobuf 的 HasField 函数需要哪些参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36742281/