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.1 (home desktop, home laptop), Crunchbang Linux 11 (work laptop), Trisquel GNU/Linux 6.0.1 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)