![](/img/trans.png)
[英]How do I assign a oneof field on a protobuf message if the child message has no fields?
[英]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.