Bad programming style?
Carel Fellinger
cfelling at iae.nl
Wed Dec 22 15:46:19 EST 1999
Grant Edwards <grant at nowhere.> wrote:
> In article <Pine.WNT.4.21.9912221356150.214-100000 at wks_3.e-shopwork.com>, Sposhua wrote:
>>I want to create a varaible name and use it as a normal variable, something
>>like:
>>
>>c=['r','g','b']
>>VARIABLE_CALLED(c[0])='ff'
>>
>>obviously there are ways around it using dictionaries or whatever, but I'd like
>>to know if it's possible to actually _create_ this variable r='ff' 'on the
>>fly'. And can anyone tell me if it actually has any use.
> Yes, you can do this in Python by creating the string "r =
> 'ff'" and then executing it with "exec" or eval(). Any
> language that can execute data can do this -- Lisp is probably
> the most (in)famous example. It's an extremely powerful tool.
Why not use the following:
>>> locals()['r'] = 'ff'
>>> print r
ff
I think this isn't garanteed to work in future versions of Python, but
it sure is crisp to me. And no hidden explosives involved at all:)
> [TNT is an extremely power tool. But, if you're planting
--
groetjes, carel
More information about the Python-list
mailing list