Variables variable

Discussion in 'Python' started by Adriano Monteiro, Apr 19, 2005.

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


    I'm FREE... Are you?
    (PYTHON powered)
     
    Adriano Monteiro, Apr 19, 2005
    #1
    1. Advertising

  2. 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
     
    Reinhold Birkenfeld, Apr 19, 2005
    #2
    1. Advertising

  3. In article <>,
    Reinhold Birkenfeld <> 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.
     
    Cameron Laird, Apr 19, 2005
    #3
  4. 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/workshops/1998-11/proceedings.html
    Army Signal Corps: Cu Chi, Class of '70
    Author: Teach Yourself Python in 24 Hours
     
    Ivan Van Laningham, Apr 20, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. avnrao
    Replies:
    0
    Views:
    3,397
    avnrao
    May 7, 2004
  2. Replies:
    1
    Views:
    964
  3. mfglinux
    Replies:
    11
    Views:
    750
    Roberto Bonvallet
    Sep 12, 2007
  4. Replies:
    9
    Views:
    1,007
  5. David Filmer
    Replies:
    19
    Views:
    279
    Kevin Collins
    May 21, 2004
Loading...

Share This Page