December 23rd, 2012, 02: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, 04: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, 09: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, 10:07 AM
If so, vars() is a safer solution:
Originally Posted by Nyktos
>>> spam = 10
My armada: openSUSE 13.2 (home laptop, work desktop), Debian GNU/Linux 8.0 (home desktop, server), Ubuntu 14.04 LTS (work laptop), OpenELEC 5.0 (HTPC), Android 4.2.1 (tablet)