考虑以下结构
message Fly {
uint32 dtime = 1;
}
但功能不起作用:
>>> 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将引发一个错误。