我有一个 ProtoBuf 对象。我想检测特定字段何时使用默认值,而不是显式提供。
message vector_measurement
{
measurement x = 1;
measurement y = 2;
measurement z = 3;
}
...
message measurement
{
...
float value = 2;
...
}
当我使用HasField
它时返回True
,但显然情况并非如此:
c = my_vector
print(c)
# x {
# value: 60.3813476562
# }
# y {
# value: 0.444311201572
# }
# z {
# }
print(c.x)
# value: 60.3813476562
print(c.z)
#
print(c.z==None)
# False
print(c.z.value)
# 0
print( c.HasField('x'), c.HasField('z') )
# (True, True )
print (c.z.HasField('value') )
# ValueError: Protocol message has no non-repeated submessage field "value"
字符串表示似乎知道z
使用默认值;我怎样才能自己检测到这一点?