Accordingly to protocol buffer python generated code documentation,
I can add a object to a repeated message field this way:
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
but:
-
how can I remove
barfrombars? -
how can I remove the
n-thbar element frombars?
It took me more than a few minutes to get the proto buffer compiler installed correctly, so that may be reason enough to ignore this 🙂
Although it isn’t in the documentation, you can actually treat a repeated field much like a normal list. Aside from its private methods, it supports
add,extend,removeandsort, andremoveis what you are looking for in the first case:Here is the output when printing
foobefore the above line (as defined by your code above) and after:As for removing the
nthelement, you can usedeland the index position you want to delete:And the output:
Hope that helps!