【问题标题】:How to assign a value to an embedded message field in Protocol buffer Python如何为协议缓冲区 Python 中的嵌入式消息字段赋值
【发布时间】:2016-11-08 11:27:27
【问题描述】:

我有以下原型,我正在尝试为嵌入的消息字段分配一个值

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

当我在 python 中编写以下代码时,它给出了以下错误

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

错误:

AttributeError: 'instancemethod' 对象没有属性 'i'

如何处理这个错误?

【问题讨论】:

  • bar 是一种方法。当你调用它时会发生什么,即bar()

标签: python protocol-buffers messages


【解决方案1】:

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

【讨论】:

  • def bar(self): 已经存在。但是我仍然面临同样的错误......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-01
  • 1970-01-01
  • 2019-03-17
  • 1970-01-01
  • 2019-02-05
相关资源
最近更新 更多