>>> a = 1
>>> ranking = "a"
>>> print 'ranking is %s' % globals()[ranking]
ranking is 1
This is more of a ugly hack and dont do it like this.
You are better of bulding a dictionary.
Something like this
>>> d = {'ranking_a': 1, 'ranking_b': 2}
>>> d['ranking_a']
1
If problems post a better description of what you are trying to do.
snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
>> my_globals = dict(a=1, b=2)
>>> ranking = ['a', 'b']
>>> x = eval("%s + %s" %(ranking[0], ranking[1]), my_globals)
>>> x
3
>>>
I bet you are not using the correct approach in your code, what you are trying to do to do this kind of indirection?
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
>>> a = 1
>>> b = 2
>>> ranking = ['a', 'b']
>>> sum(globals()[i] for i in ranking)
3
Like this,but not nice.
You see PyTony make a dictionary and than take out values.
But you have to rethink this.
string 'a' and 'b' in ranking is just string not pointing to anything.
You can bind string 'a' 'b' to variable a b.
>>> d = {}
>>> d[ranking[0]] = a
>>> d[ranking[1]] = b
>>> d
{'a': 1, 'b': 2}
Or just make a dictionary in first place,not confusing stuff like now.
snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294