Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape reserved keyword in Python [Protocol Buffer autogenerated class]

so I'm sort of in a fix. I'm using Google protocol buffers, and it just so happens that one of the fields in the schema is named "from". I'm using python, so everytime I try to access it, I get a syntax error. [ex - SomeClass.from -> Syntax error ]

Is there anyway to somehow access the field without using its identifier?

Maybe a way to escape reserved keywords in Python ? (One of the answers already says no, but...)

Or maybe some protobuf specific solution?

Thanks

like image 354
Aneesh Barthakur Avatar asked Nov 27 '25 08:11

Aneesh Barthakur


1 Answers

After you pull your data, you can always save the from into from_ (the pythonic way of avoiding namespace clashes) by using the getattr(var, "from") statement; Ie

SomeClass # is a protocol-buffer
SomeClass.from_ = getattr(SomeClass, "from")

And then you an just use .from_ as you would otherwise.

like image 89
Delioth Avatar answered Nov 29 '25 22:11

Delioth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!