[Python] Variable variables?

larry price laprice at gmail.com
Sun Mar 18 16:01:16 PDT 2007


On 3/18/07, Martin Kelly <aomighty at gmail.com> wrote:
> If it's the second example, eval will work but there's usually cleaner
> ways to do it... I remember needing it once but I replaced it with
> something more understandable.

why is everybody making this complicated. you can refer to the same
object by many different names, and using a loop variable you can
refer to many different objects with the same name.

>>> l1 = ['b','r','t']
>>> l2 = ['t','b','a']
>>> l3 = ['f','b','i']
>>> l = [l1,l2,l3]
>>> for t in l: print t
...
['b', 'r', 't']
['t', 'b', 'a']
['f', 'b', 'i']
>>> for t in l:
...  t.sort()
...  print t
...
['b', 'r', 't']
['a', 'b', 't']
['b', 'f', 'i']


More information about the Python mailing list