gpt4 book ai didi

python - proto3 Python 中未显示 False Boolean

转载 作者:行者123 更新时间:2023-12-04 15:29:14 25 4
gpt4 key购买 nike

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

entity_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这在某种程度上没问题。当我使用 Http trancoding 时,主要问题出现了如果我尝试打印 console.log(foo.active)是给我 undefined不是 false (语言:JavaScript)

有人可以告诉我为什么它不显示 False值。

最佳答案

Prototbuf 具有字段的默认值,例如 bool 假、数字零或空字符串。

它不会打扰编码,因为它浪费空间和/或带宽(传输时)。这可能就是它没有出现的原因。

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

foo = entity_proto.Foo(id='', active=True)
print(foo)
# active: True (I suspect).

解决方案实际上取决于 undefined来自。 Javascript 有一个真正的 undefined 值,在这种情况下,您可以使用 null/undefined 合并运算符:
console.log(foo.active ?? false)

或者,如果此 HTTP 转码器正在执行类似创建文字“未定义”字符串之类的操作,则您必须弄清楚如何转(可能是) None变成“假”。

关于python - proto3 Python 中未显示 False Boolean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61606095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com