Variable names on the fly?

Gonçalo Rodrigues op73418 at mail.telepac.pt
Thu Nov 14 15:10:53 EST 2002


On 14 Nov 2002 19:51:25 GMT, Gerhard Häring <gerhard.haering at gmx.de>
wrote:

>Gonçalo Rodrigues wrote:
>> On Thu, 14 Nov 2002 19:13:22 -0000, "e-tones.co.uk"
>>>[...] Whats the proper format to concatenate (sp?) variable names. I
>>>used list+i, obviously this doesnt work, but what does :)
>> 
>> As far as I know you cannot do this [...]
>
>There's little you cannot do in Python ;-)
>
>for i in range(3):
>    locals()["list%i" % i] = []

Although I'm sure you are aware of it, here is for general information:

>From the docs on locals:
"Return a dictionary representing the current local symbol table.
Warning: The contents of this dictionary should not be modified; changes
may not affect the values of local variables used by the interpreter."

>
>... it's actually a very bad idea. Use nested lists or a dictionary.
>
>OTOH being able to dynamically create class attributes by writing in
>the class object's __dict__ or by overriding __getattr__ sometimes
>really helps.
>
>-- Gerhard

All the best,
Gonçalo Rodrigues



More information about the Python-list mailing list