By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,665 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

Variables variable

P: n/a
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
py********@gmail.com

I'm FREE... Are you?
(PYTHON powered)
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

Reinhold
Jul 19 '05 #2

P: n/a
In article <3c*************@individual.net>,
Reinhold Birkenfeld <re************************@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.

Jul 19 '05 #3

P: n/a
Hi All--

Cameron Laird wrote:
.
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/worksh...oceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.