Variable names on the fly?

Gerhard Häring gerhard.haering at gmx.de
Thu Nov 14 14:51:25 EST 2002


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] = []

... 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



More information about the Python-list mailing list