问题
How can I programatically set a field value by its name for a google protobuffer object in python. I've checked the documentation and API, but its just not clear to me, and I haven't been able to find any examples.
I'd like to do do something like:
protoBufObj.set_field('foo', 42)
Thanks.
回答1:
If you've got any Python object where x.foo = 42
works, setattr(x, 'foo', 42)
should have precisely the same effect. This is a built-in Python function, nothing to do with protobuffers.
来源:https://stackoverflow.com/questions/10456399/using-google-protobuffers-reflection-in-python