Variables variable
Cameron Laird
claird at lairds.us
Tue Apr 19 18:08:03 EDT 2005
In article <3ckeukF6n53cpU1 at individual.net>,
Reinhold Birkenfeld <reinhold-birkenfeld-nospam at wolke7.net> wrote:
>Adriano Monteiro wrote:
>> Hi folks,
>>
>> Someone know how to make variables variable like in PHP?
>> It's something like this:
>>
>> $a = 'hi'
>> $$a = 'testing'
>> echo $hi
>> 'testing'
>
>You are most certainly wanting to use dictionaries.
>
>Or, if you work with attributes of an object, use getattr.
.
.
.
I have a few comments on this: Python can do essentially what
PHP does. One means is the getattr() Reinhold cites.
HOWEVER, it's generally always a mistake to do so. Dictionaries
are simply better. AND THAT'S TRUE IN PHP, too. I'm frankly
disappointed in PHP leaders that they continue to promote this
"variable-variable" idiom. As much as anything is certain in
the realm of stylistic expression, dictionaries are a superior
mechanism.
More information about the Python-list
mailing list