Re: A variables variables

2008-08-26 Thread Bruno Desthuilliers

castironpi a écrit :

On Aug 23, 7:25 pm, Gandalf [EMAIL PROTECTED] 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


Please avoid this kind of yucky ugly hacks.

--
http://mail.python.org/mailman/listinfo/python-list


Re: A variables variables

2008-08-24 Thread Maric Michaud
Le Sunday 24 August 2008 03:24:29 Terry Reedy, vous avez écrit :
 Gandalf 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

 Others have given you the direct answer.

Well using locals() is the right answer, it is a very bad use case for eval.

 But using lists or dicts is 
 almost always a better solution in Python than synthesizing global/local
 namespace names.

 a={'hello':'baybay'}
 print a['hello']

But locals() *is* an already made dict, very convenient for use locally and 
read-only.

[155]: var1 = easy

[156]: var2 = proper

[157]: var3 = locals

[158]: print a %(var2)s and %(var1)s use of %(var3)s with %(var3)s() % 
locals()
a proper and easy use of locals with locals()


-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


A variables variables

2008-08-23 Thread Gandalf
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!
--
http://mail.python.org/mailman/listinfo/python-list


Re: A variables variables

2008-08-23 Thread castironpi
On Aug 23, 7:25 pm, Gandalf [EMAIL PROTECTED] 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.
--
http://mail.python.org/mailman/listinfo/python-list


Re: A variables variables

2008-08-23 Thread Mohamed Yousef
i don't know if this volatiles array condition or not
but any way

q='asd'
asdasd=20
print globals()[q+'asd']
--
http://mail.python.org/mailman/listinfo/python-list


Re: A variables variables

2008-08-23 Thread Benjamin
On Aug 23, 7:25 pm, Gandalf [EMAIL PROTECTED] 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

Doing this sort of thing in Python is very anti idiom.

 how can i do it with no Arrays using  python

Why would you not want to use a list? I see no reason not to.

 thanks!

--
http://mail.python.org/mailman/listinfo/python-list


Re: A variables variables

2008-08-23 Thread Terry Reedy



Gandalf 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


Others have given you the direct answer.  But using lists or dicts is 
almost always a better solution in Python than synthesizing global/local 
namespace names.


a={'hello':'baybay'}
print a['hello']

--
http://mail.python.org/mailman/listinfo/python-list


Re: A variables variables

2008-08-23 Thread Gandalf
OK thank you all !

sometimes you just getting use to somethings...
--
http://mail.python.org/mailman/listinfo/python-list