繁体   English   中英

如何为 protobuf 中的可选消息字段赋值?

[英]How to assign values to an optional message field in protobuf?

我有以下 proto 文件

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

我可以知道为什么它会抛出这个错误吗? 另外,如何为“可选”消息字段赋值?

尝试在此处删除括号: id = message.id()id = message.id

另一种方法来做到这一点:

thisid = ID()
thisid.id_upper = 10
thisid.id_lower = 20
message = mymessage()
message.id = thisid

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM