2016-11-08 159 views
0

值分配给嵌入式消息字段我有以下原,我想如果我用Python写下面的代码值赋给一个嵌入式消息字段如何在协议缓冲区的Python

message Foo { 
    required Bar bar = 1; 
} 
message Bar { 
    optional int32 i = 1; 
} 

,它提供了以下错误

foo = Foo() 
foo.bar.i = 1 

错误:

AttributeError: 'instancemethod' object has no attribute 'i'

如何处理这个问题?

+0

'bar'是一种方法。当你调用它时会发生什么,例如'bar()' –

回答

0

要在Python中执行您想要的操作,您必须在Foo类中定义bar方法。像这样的事情会做到这一点:

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 
+0

def bar(self):已经存在。不过,我仍然面临同样的错误... – Venkatesh