Migrating from PHP to Python...

Ignacio Vazquez-Abrams ignacio at openservices.net
Tue Sep 4 00:08:47 EDT 2001


On Tue, 4 Sep 2001, Joal Heagney wrote:

> Rick Shin wrote:
> >
> > >
> > > All variables in python are dynamic type. Is that what you were asking?
> > >
> > > >>> a=1
> > > >>> a
> > > 1
> > > >>> a = 'hello'
> > > >>> a
> > > 'hello'
> >
> > Actually, I was referring to PHP's ability to assign names dynamically to
> > variables. For example:
> >
> > $var = 'hello';
> > $$var = 'world!';
> > echo $var . ' ' . $$var;    // prints 'hello world!'
> > echo $var . ' ' . $hello;    // prints 'hello world!' too
>
> You mean like this?
>
>  [snip]
>

Sorry Joal, Rick meant exactly what he wrote.

It's just that the concept of a variable variable name usually isn't used in
programming, so when most people encounter it, they feel that there's
something wrong with the code.

You can simulate it in Python using globals():

---
>>> var='hello'
>>> globals()[var]='world!'
>>> print '%s %s' % (var, globals()[var])
hello world!
>>> print '%s %s' % (var, hello)
hello world!
>>>
---

-- 
Ignacio Vazquez-Abrams  <ignacio at openservices.net>






More information about the Python-list mailing list