简体   繁体   English

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

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

I have below proto file我有以下 proto 文件

syntax = "proto3";
package my_proto;
message ID
{
   optional uint64 id_upper = 1;
   optional uint64 id_lower = 2;
}
message mymessage
{
   optional ID id = 1;

}

And I tried assigning value to ID field using below python script我尝试使用下面的 python 脚本为 ID 字段赋值

import my_proto_pb2

message = my_proto_pb2.mymessage()
id = message.id()
id.id_upper = 10
id.id_lower = 20

But I got below error:但我得到以下错误:

Traceback (most recent call last):
  File "create_message.py", line 4, in <module>
    id = message.id()
TypeError: 'ID' object is not callable

May I know why it is throwing this error?我可以知道为什么它会抛出这个错误吗? Also, how can I assign value to an 'optional' message field?另外,如何为“可选”消息字段赋值?

Try removing parenthesis here: id = message.id() to id = message.id尝试在此处删除括号: id = message.id()id = message.id

another way to do this:另一种方法来做到这一点:

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