def gv(s):
return globals()[s]
print gv(x[0])
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
This came up recently and I wan to say that there is 99% chance that you are doing something stupid. Usually one would use dictionary, like
import string
d = dict((a,c) for c,a in enumerate(string.lower, 1))
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
If any letter in "x" has global variable, it will use global variable, else it will just the letter unchanged.
Here it is
def gv(s):
return globals().get(s, s)
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
Here we go again:ooh:
>>> a = 1
>>> globals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__doc__': None, 'a': 1, '__package__': None}
What`s happen here is that we take a look into the global namespace.
We see that global namespace store 'a': 1 as a dictionary.
Let create a visible dictionary.
>>> d = {'a': 1}
Now we shall use both.
>>> globals()['a']
1
>>> d['a']
1
>>>
So a advice dont use python internal working like global namespace as a dictionary.
The code get much clearer if you make a ordinary dictionary.
snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294