Variable Variable

Mike Gould mike at meph.net
Sun Mar 20 11:25:12 EST 2005


Premshree Pillai wrote:
> On Sat, 19 Mar 2005 04:35:47 -0500, Leif K-Brooks <eurleif at ecritters.biz> wrote:
> 
>>Tanteauguri wrote:
>>
>>>Hi List, is there in python a variable variable like in PHP ($$var)?
>>>
>>>What I want to do is something like that:
>>>
>>>pc=["a","b","c"]
>>>
>>>for i in pc:
>>>    i = anyclass()
>>>
>>>a.shutdown()
>>>b.update()
>>
>>Use a dictionary:
>>
>>stuff = {}
>>pc = ['a', 'b', 'c']
> 
> 
> I think what he wants to do is basically hold object names in a tuple
> list, and then call the methods on those objects (while iterating or
> whatever).
> 
> 
>>for i in pc:
>>     stuff[i] = anyclass()
>>
>>stuff['a'].shutdown()
>>stuff['b'].update()
>>--
>>http://mail.python.org/mailman/listinfo/python-list
>>
> 

Try this:

pc=["a","b","c"]
for i in pc:
     vars()[i] = anyclass()
a.shutdown()
b.update()

MikeG





More information about the Python-list mailing list