根据协议缓冲区python 生成的代码文档,我可以通过这种方式将对象添加到重复的消息字段中:
foo = Foo()
bar = foo.bars.add() # Adds a Bar then modify
bar.i = 15
foo.bars.add().i = 32 # Adds and modify at the same time
但:
我怎样才能从中
bar删除bars?如何从中删除
n-thbar 元素bars?
根据协议缓冲区python 生成的代码文档,我可以通过这种方式将对象添加到重复的消息字段中:
foo = Foo()
bar = foo.bars.add() # Adds a Bar then modify
bar.i = 15
foo.bars.add().i = 32 # Adds and modify at the same time
但:
我怎样才能从中bar删除bars?
如何从中删除n-thbar 元素bars?
我花了几分钟才正确安装了 proto buffer 编译器,所以这可能足以忽略这一点:)
虽然它不在文档中,但您实际上可以像对待普通列表一样对待重复字段。除了它的私有方法之外,它还支持add,和extend,并且是您在第一种情况下要寻找的:removesortremove
foo.bars.remove(bar)
foo这是在上述行之前(由上面的代码定义)和之后打印时的输出:
Original foo: bars { i: 15 } bars { i: 32 } foo without bar: bars { i: 32 }
至于删除nth元素,可以使用del和要删除的索引位置:
# Delete the second element
del foo.bars[1]
和输出:
Original foo: bars { i: 15 } bars { i: 32 } Removing index position 1: bars { i: 15 }
希望有帮助!