简体   繁体   中英

How to assign values to an optional message field in protobuf?

I have below proto file

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

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

another way to do this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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