简体   繁体   English

如何在Python中设置一个空消息的ProtoBuf字段?

[英]How to set a ProtoBuf field which is an empty message in Python?

The following are the contents of the Google Protocol Buffer (.proto) file 以下是Google Protocol Buffer(.proto)文件的内容

message First
{
    required uint32 field1 = 1;

    optional MessageType1 request = 2;
}

message MessageType1
{
}

I want to set the MessageType1 field request. 我想设置MessageType1字段请求。 But I get this as an error: 但我认为这是一个错误:

AttributeError: Assignment not allowed to composite field "request" in protocol message object.

How to set the value of this empty message in Python? 如何在Python中设置此空消息的值?

Got this in the source code of Message class in Proto Buffer. 在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."""

So the way to set such an empty message is to call this function: 所以设置这样一个空消息的方法是调用这个函数:

first.request.SetInParent()

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

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