473,503 Members | 12,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Variable by label

Is there any function which will return a variable by passing to it
the string containing the variable's name? Something like this for
instance:

foo = some_function("foo")

or perhaps it's a dictionary. I don't know, but I would really like to
find out how it can be done. I guess it is achievable.

Thank you.
Jun 27 '08 #1
5 1028


rocco.ro...@gmail.com wrote:
Is there any function which will return a variable by passing to it
the string containing the variable's name? Something like this for
instance:

foo = some_function("foo")

or perhaps it's a dictionary. I don't know, but I would really like to
find out how it can be done. I guess it is achievable.

Thank you.
There are the globals() and locals() builtins.
>>a = 42
globals()['a']
42
>>>
--
Arnaud
Jun 27 '08 #2
ro*********@gmail.com wrote:
Is there any function which will return a variable by passing to it
the string containing the variable's name? Something like this for
instance:

foo = some_function("foo")

or perhaps it's a dictionary. I don't know, but I would really like to
find out how it can be done. I guess it is achievable.

Thank you.
vars()["foo"]

but if you are doing this you are almost certainly making your life harder
than it needs to be. Usually you really just want to keep the values in a
dict.

--
Duncan Booth http://kupuguy.blogspot.com
Jun 27 '08 #3
ro*********@gmail.com pisze:
Is there any function which will return a variable by passing to it
the string containing the variable's name? Something like this for
instance:

foo = some_function("foo")

or perhaps it's a dictionary. I don't know, but I would really like to
find out how it can be done. I guess it is achievable.
getattr(object, attrname, default)

--
Jarek Zgoda
Skype: jzgoda | GTalk: zg***@jabber.aster.pl | voice: +48228430101

"We read Knuth so you don't have to." (Tim Peters)
Jun 27 '08 #4
>>a = 1
>>b = eval("a")
b
1
>>a =1
b = globals()["a"]
b
1
>>a =1
b = locals()["a"]
b
1
<ro*********@gmail.comwrote in message
news:ae**********************************@b1g2000h sg.googlegroups.com...
Is there any function which will return a variable by passing to it
the string containing the variable's name? Something like this for
instance:

foo = some_function("foo")

or perhaps it's a dictionary. I don't know, but I would really like to
find out how it can be done. I guess it is achievable.

Thank you.

Jun 27 '08 #5
On May 16, 4:27 pm, "inhahe" <inh...@gmail.comwrote:
>a = 1
b = eval("a")
b
1
>a =1
b = globals()["a"]
b
1
>a =1
b = locals()["a"]
b

1<rocco.ro...@gmail.comwrote in message

news:ae**********************************@b1g2000h sg.googlegroups.com...
Is there any function which will return a variable by passing to it
the string containing the variable's name? Something like this for
instance:
foo = some_function("foo")
or perhaps it's a dictionary. I don't know, but I would really like to
find out how it can be done. I guess it is achievable.
Thank you.
Perfect. Thank you all for your replies.
Jun 27 '08 #6

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

Similar topics

4
6285
by: Dan | last post by:
Can anyone offer suggestions on how to do this or if it is possible? I have a form that uses a drop down box and 2 text fields. What I am trying to do is have the value of each text box set by...
6
3503
by: utterberg | last post by:
Hi, I've got an xml that looks like this: <section name="Risk"> <label>RISK MANAGEMENT (RM)</label> <field name="RM.1"> <ref>RM.1</ref> <label>Text goes here...</label> <option...
0
1822
by: beanweed | last post by:
BACKGROUND ---------- I have an ASP.NET application having two panels. In one panel, an XML document, transformed using xsl, is displayed. In the other panel are some controls that allow a user...
7
1459
by: Jeff Johnson | last post by:
Hi Everyone, I have a page with numerous label controls with the Visible attribute set to false. On page_load I loop through an array and depending on what is returned for each label, I want...
5
1972
by: Michelle A. | last post by:
I have a four page form. Pages 1-3 stores there information in a session variables. Page four is reached (a final review page) and then the information will be written to the SQL server. When...
2
3618
by: Jim | last post by:
I have a C# Web application that has 1 Web form that has 2 command buttons on it with a label for output. I have a form level variable declared String strName = "Jim"; right below where the...
7
2177
by: Aaron | last post by:
Complete code follows. I am new to .NET programming (and programming in general) and I am having a difficult time understanding how to fill a variable in one sub, and then access it from...
1
3427
by: Arthur Dent | last post by:
Hello all, sorry for the cross-post, but im not sure which group is best for this question. I am an ASP.NET developer, but am learning PHP/perl for the first time now to make some to changes to...
4
1526
by: Phillip Vong | last post by:
I'm using VS2005 and creating a simple test ASPX page in VB. I have a simple FORMVIEW1 with a label "YTDLabel" databound to a SQL DB. The DB datatype is set to decimal (18,2) and the value of...
14
13025
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; int main() { int i;
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6975
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5562
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.