如何为 protobuf 中的可选消息字段赋值?
How to assign values to an optional message field in protobuf?
我有以下原型文件
syntax = "proto3";
package my_proto;
message ID
{
optional uint64 id_upper = 1;
optional uint64 id_lower = 2;
}
message mymessage
{
optional ID id = 1;
}
我尝试使用下面的 python 脚本为 ID 字段赋值
import my_proto_pb2
message = my_proto_pb2.mymessage()
id = message.id()
id.id_upper = 10
id.id_lower = 20
但是我得到以下错误:
Traceback (most recent call last):
File "create_message.py", line 4, in <module>
id = message.id()
TypeError: 'ID' object is not callable
我可以知道为什么会抛出这个错误吗?另外,如何为 'optional' 消息字段赋值?
尝试在此处删除括号:
id = message.id()
至 id = message.id
另一种方法:
thisid = ID()
thisid.id_upper = 10
thisid.id_lower = 20
message = mymessage()
message.id = thisid
我有以下原型文件
syntax = "proto3";
package my_proto;
message ID
{
optional uint64 id_upper = 1;
optional uint64 id_lower = 2;
}
message mymessage
{
optional ID id = 1;
}
我尝试使用下面的 python 脚本为 ID 字段赋值
import my_proto_pb2
message = my_proto_pb2.mymessage()
id = message.id()
id.id_upper = 10
id.id_lower = 20
但是我得到以下错误:
Traceback (most recent call last):
File "create_message.py", line 4, in <module>
id = message.id()
TypeError: 'ID' object is not callable
我可以知道为什么会抛出这个错误吗?另外,如何为 'optional' 消息字段赋值?
尝试在此处删除括号:
id = message.id()
至 id = message.id
另一种方法:
thisid = ID()
thisid.id_upper = 10
thisid.id_lower = 20
message = mymessage()
message.id = thisid