簡體   English   中英

如何為協議緩沖區Python中的嵌入消息字段賦值

[英]How to assign a value to an embedded message field in Protocol buffer Python

我有跟隨proto,我正在嘗試為嵌入的消息字段分配值

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

當我在python中編寫下面的代碼時,它給出了以下錯誤

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

錯誤:

AttributeError:'instancemethod'對象沒有屬性'i'

如何處理這個錯誤?

要做到你想要的,在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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM