[英]How to assign a value to an embedded message field in Protocol buffer Python
I have following proto and I am trying to assign a value to an embedded message field 我有跟随proto,我正在尝试为嵌入的消息字段分配值
message Foo {
required Bar bar = 1;
}
message Bar {
optional int32 i = 1;
}
When I am writing following code in python, It gives below error 当我在python中编写下面的代码时,它给出了以下错误
foo = Foo()
foo.bar.i = 1
Error: 错误:
AttributeError: 'instancemethod' object has no attribute 'i' AttributeError:'instancemethod'对象没有属性'i'
How to deal with this error? 如何处理这个错误?
To do what you want, in Python, you have to define the bar
method within the Foo
class. 要做到你想要的,在Python中,你必须在Foo
类中定义bar
方法。 Something like this will do it: 像这样的东西会这样做:
class Foo:
i = 1
def bar(self):
return self.i
if __name__ == '__main__':
foo = Foo()
foo.bar = 1
print(foo.bar) # this will print 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.