繁体   English   中英

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

[英]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.

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