Variables variable

A

Adriano Monteiro

Hi folks,

Someone know how to make variables variable like in PHP?
It's something like this:

$a = 'hi'
$$a = 'testing'
echo $hi
'testing'

Regards

--

Adriano Monteiro Marques
www.gopython.com.br
(e-mail address removed)

I'm FREE... Are you?
(PYTHON powered)
 
R

Reinhold Birkenfeld

Adriano said:
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.

Reinhold
 
C

Cameron Laird

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

Ivan Van Laningham

Hi All--

Cameron said:
.
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.

I don't know if this dictionary insight is Cameron's, but my experience
with Python has been that when I'm developing something from scratch,
I'll start with a big pile of cruft. "I think I'll need this. Oh, and
that, too. And what about this! Oooh, cool!" Then I start carving
away the excess. It usually takes two or three iterations at carving to
realize that whatever it is needs a dictionary. I have never seen a
case where a dictionary didn't improve the design. +1QOTW.

Well. Storing a Mayan number/date as a dictionary turned out to be a
Spectacularly Bad Idea(tm);-), so I guess that's one case.

Metta,
Ivan
----------------------------------------------
Ivan Van Laningham
God N Locomotive Works
http://www.andi-holmes.com/
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top