473,322 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

A variables variables

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!
Aug 24 '08 #1
7 1037
On Aug 23, 7:25*pm, Gandalf <goldn...@gmail.comwrote:
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.
Aug 24 '08 #2
i don't know if this volatiles array condition or not
but any way

q='asd'
asdasd=20
print globals()[q+'asd']
Aug 24 '08 #3
On Aug 23, 7:25*pm, Gandalf <goldn...@gmail.comwrote:
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!
Aug 24 '08 #4


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']

Aug 24 '08 #5
OK thank you all !

sometimes you just getting use to somethings...
Aug 24 '08 #6
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
Aug 24 '08 #7
castironpi a écrit :
On Aug 23, 7:25 pm, Gandalf <goldn...@gmail.comwrote:
>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.

Aug 26 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Hal Vaughan | last post by:
First, I am aware of both SwingUtilities.invokeLater(), and of using Thread to create a new thread.  These are part of the problem. I want to have something running in the background, while the...
4
by: Torsten Bronger | last post by:
Hallöchen! I have a file that looks a little bit like a C header file with a long list of variables (actually constants) definitions, e.g. VI_ATTR_TIMO = 0x54378 .... Actually I need this...
1
by: mark4asp | last post by:
What are the best methods for using global constants and variables? I've noticed that many people put all global constants in a file and include that file on every page. This is the best way of...
5
by: Ross A. Finlayson | last post by:
Hi, I'm scratching together an Access database. The development box is Office 95, the deployment box Office 2003. So anyways I am griping about forms and global variables. Say for example...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
7
by: misha | last post by:
Hello. I was wandering if someone could explain to me (or point to some manual) the process of mapping the addresses of host variables by DB2. Especially I would like to know when DB2 decides to...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
4
by: icarus | last post by:
global_vars.py has the global variables set_var.py changes one of the values on the global variables (don't close it or terminate) get_var.py retrieves the recently value changed (triggered right...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.