Re: How to add a repeated field via reflection, in Python?

411 views
Skip to first unread message

JeffD

unread,
Jun 8, 2012, 4:35:47 PM6/8/12
to prot...@googlegroups.com
On Friday, June 8, 2012 10:56:12 AM UTC-7, JeffD wrote:
#!/usr/bin/env python

import foobar_pb2

# protoc -I=. --python_out=. foobar.proto

# message Bar {
#   optional int32 i = 1 [default = 0];
#   optional int32 j = 2 [default = 0];
# }
# message Foo {
#   repeated Bar bars = 1;
# }

foo = foobar_pb2.Foo()
# ok, but not reflection
#foo.bars.add()

got it:

bars = getattr(foo, 'bars')
bars.add()

bars[0].i = 37

print foo.bars[0]

# prints i: 37

Reply all
Reply to author
Forward
0 new messages