Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
proto3 Python中未显示False Boolean_Python_Protocol Buffers - Fatal编程技术网

proto3 Python中未显示False Boolean

proto3 Python中未显示False Boolean,python,protocol-buffers,Python,Protocol Buffers,我在python中使用protoBuffer 3,它没有正确显示bool字段 实体_proto message Foo { string id = 1; bool active = 3; } 在python中设置值 foo = entity_proto.Foo(id='id-123', active=True) print(foo) # id: id-123 # active: True # But if you set the value False it does not

我在
python
中使用
protoBuffer 3
,它没有正确显示
bool
字段

实体_proto

message Foo {
    string id = 1;
    bool active = 3;
}
在python中设置值

foo = entity_proto.Foo(id='id-123', active=True)
print(foo)
# id: id-123
# active: True

# But if you set the value False it does not show 'active' in print statement
foo = entity_proto.Foo(id='id-123', active=False)
print(foo)
# id: id-123
如果您尝试打印
print(foo.active)
输出为
False
,这在某种程度上是正常的。当我试图打印
console.log(foo.active)
时,使用
httptrancoding
会出现主要问题。log(foo.active)是give me
未定义的
(lang:JavaScript)

有人能告诉我为什么它没有显示为
False
值吗

Prototbuf具有字段的默认值,例如布尔值false、数字零或空字符串

它不需要对它们进行编码,因为这样会浪费空间和/或带宽(在传输时)。这可能就是它没有出现的原因

检查这一点的一个好方法是将
id
设置为空字符串,并查看其行为是否类似:

foo = entity_proto.Foo(id='', active=True)
print(foo)
# active: True (I suspect).
解决方案实际上取决于
未定义的
来自何处。任一Javascript都有一个真正的未定义值,在这种情况下,您可以使用null/undefined合并运算符:

console.log(foo.active ?? false)

或者,如果这个HTTP转码器正在做一些类似于创建一个文本“未定义”字符串的事情,那么您必须根据protobuf语言指南,找出如何将(可能是什么)
None
转换为“false”。

还请注意,如果标量消息字段设置为其默认值,则不会在线路上序列化该值

因此,对于布尔字段,如果其默认值为,则它甚至不会序列化。您的另一个选项是设置一些int/string字段并将其设置为某个值,以便您可以决定您的逻辑