December 23rd, 2012, 03:56 AM
String to Constant conversion
I am a new python programmer. Can anybody please tell me how can I convert a string to constant: for example i have a string
"Key.Enter" I want to convert it to just Key.ENTER constant. Thts is
"Key.ENTER" --> Key.ENTER
Plz help me
December 23rd, 2012, 05:09 AM
There's no equivalent for constants in Python, as the programmer is generally considered intelligent enough to leave a value he/she wants to stay constant alone.
December 23rd, 2012, 10:41 AM
I think what he means is, given a string containing a name, he wants the object with that name.
Calling eval in the appropriate scope will do the trick, but chances are there's a less ugly solution given context.
December 23rd, 2012, 11:07 AM
If so, vars() is a safer solution:
Originally Posted by Nyktos
>>> spam = 10
My armada: Debian GNU/Linux 8 (desktop, home laptop, work laptop), Raspbian GNU/Linux 8 (nameserver), Ubuntu 14.04.3 LTS (HTPC), PC-BSD 10.2 (testbed), Android 4.2.1 (tablet)