A variables variables

castironpi castironpi at gmail.com
Sat Aug 23 20:36:43 EDT 2008


On Aug 23, 7:25 pm, Gandalf <goldn... at gmail.com> wrote:
> how can I declare a variable with another variable  name?
>
> for example  I will use PHP:
>
> $a= "hello";
>
> $a_hello="baybay";
>
> print ${'a_'.$a)  //output: baybay
>
> how can i do it with no Arrays using  python
>
> thanks!

Here's one idea.

>>> a= 'hello'
>>> a_hello= 'bayb'
>>> print eval( 'a_'+ a )
bayb
>>>

Also, you can look up locals() and globals(), or getattr, if you do
this in a class.



More information about the Python-list mailing list