![](/img/trans.png)
[英]How to distinguish between a protobuf repeated field being explicitly set to empty list vs. not set at all (in python)?
[英]How to set a ProtoBuf field which is an empty message in Python?
以下是Google Protocol Buffer(.proto)文件的內容
message First
{
required uint32 field1 = 1;
optional MessageType1 request = 2;
}
message MessageType1
{
}
我想設置MessageType1字段請求。 但我認為這是一個錯誤:
AttributeError: Assignment not allowed to composite field "request" in protocol message object.
如何在Python中設置此空消息的值?
在Proto Buffer的Message類的源代碼中得到了這個。
def SetInParent(self):
"""Mark this as present in the parent.
This normally happens automatically when you assign a field of a
sub-message, but sometimes you want to make the sub-message
present while keeping it empty. If you find yourself using this,
you may want to reconsider your design."""
所以設置這樣一個空消息的方法是調用這個函數:
first.request.SetInParent()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.