472,977 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Retrieving a variable's name.

How would I go about retrieving a variable's name (not its value)? I
want to write a function that, given a list of variables, returns a
string with each variable's name and its value, like:

a: 100
b: 200

I get the feeling this is trivial, but I have been unable to find an
answer on my own.

Thanks,

Rodrigo

Aug 21 '07 #1
7 1523
On 20 ago, 22:03, rodrigo <rodrigo...@gmail.comwrote:
How would I go about retrieving a variable's name (not its value)? I
want to write a function that, given a list of variables, returns a
string with each variable's name and its value, like:

a: 100
b: 200

I get the feeling this is trivial, but I have been unable to find an
answer on my own.
Try using dir(), dir(xxx), vars(), vars(xxx), locals(), globals()
<http://docs.python.org/lib/built-in-funcs.html>

--
Gabriel Genellina

Aug 21 '07 #2
On 8/20/07, rodrigo <ro********@gmail.comwrote:
How would I go about retrieving a variable's name (not its value)? I
want to write a function that, given a list of variables, returns a
string with each variable's name and its value, like:

a: 100
b: 200
Let me preface my response by saying that this is a really weird thing
to do, and almost certainly _not_ what you want to be doing. If I was
to run across code like this, I'd be appalled ;-) Here's one way to
do it, however:

def make_dict(*args):
d = {}
for arg in args:
for key, value in globals().iteritems():
if value is arg:
d[key] = value
break
return d

Note that this returns a dictionary, rather than a string, but this is
trivial to modify.

--
Evan Klitzke <ev**@yelp.com>
Aug 21 '07 #3
rodrigo <ro********@gmail.comwrites:
How would I go about retrieving a variable's name (not its value)? I
want to write a function that, given a list of variables, returns a
string with each variable's name and its value, like:

a: 100
b: 200

I get the feeling this is trivial, but I have been unable to find an
answer on my own.
Why do you want to do that? The reason you haven't found an answer is
that it's almost certainly not the right solution to whatever problem
you're trying to solve.

'\n'.join('%s: %s'%(k,v) for k,v in locals().iteritems())

(untested) does something like what you're asking but I don't
advise it. You should probably use a dictionary object instead.
Aug 21 '07 #4
On 8/20/07, Evan Klitzke <ev**@yelp.comwrote:
On 8/20/07, rodrigo <ro********@gmail.comwrote:
How would I go about retrieving a variable's name (not its value)? I
want to write a function that, given a list of variables, returns a
string with each variable's name and its value, like:

a: 100
b: 200

Let me preface my response by saying that this is a really weird thing
to do, and almost certainly _not_ what you want to be doing. If I was
to run across code like this, I'd be appalled ;-) Here's one way to
do it, however:

def make_dict(*args):
d = {}
for arg in args:
for key, value in globals().iteritems():
if value is arg:
d[key] = value
break
return d
On second thought, this won't really work, because you can have
multiple names for the same value in Python. For example, if we say a
= 2 and d = 2, it will be true that a is d (since only one copy of
small integers is made) and thus it is not possible to distinguish
whether the name that was passed in to the function is a or d.

You should just use a dictionary in your code in the first place,
rather than trying to make one out of a list of variable names and
values.

--
Evan Klitzke <ev**@yelp.com>
Aug 21 '07 #5
You're right, Paul, Evan, James, I should just use a dictionary.

Thanks!

Rodrigo

Aug 21 '07 #6
rodrigo wrote:
How would I go about retrieving a variable's name (not its value)? I
want to write a function that, given a list of variables, returns a
string with each variable's name and its value, like:

a: 100
b: 200

I get the feeling this is trivial, but I have been unable to find an
answer on my own.
So what would you want

yourfunction(32)

to return? None?

Names in Python are references to values, and the same value can be
reference by zero to many different names, so your request is close to
nonsensical.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Aug 21 '07 #7
On 8/21/07, rodrigo <ro********@gmail.comwrote:
How would I go about retrieving a variable's name (not its value)?
http://effbot.org/pyfaq/how-can-my-c...-an-object.htm

--
Cheers,
Simon B.
si***@brunningonline.net
http://www.brunningonline.net/simon/blog/
GTalk: simon.brunning | MSN: small_values | Yahoo: smallvalues
Aug 21 '07 #8

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

Similar topics

2
by: Sven Wynsberghe | last post by:
Like the topic states, is there any way I can get the ip-adress of my computer with a vb.net application? thanks for the help :)
5
by: jan axelson | last post by:
My application is using RegisterDeviceNotification() to detect attachment and removal of a USB HID-class device. The form is receiving WM_DEVICECHANGE messages with wParam set to...
0
by: Andy | last post by:
Hi All. I'm working for a company that has set out a guideline for retrieving data from a database. Nobody can explain to me the reason for the following. When retrieving a set of records...
10
by: Girish | last post by:
Hi Everyone, I am passing a form to a php script for further processing. I am able to retrieve the last value set for that given form variable using $variable=$_REQUEST;
4
by: namanhvu | last post by:
Hi everyone, I am trying to create a form for authorised users to upload photos to an image gallery. I'm attempting to perform simple validation of the fields by ensuring the fields aren't empty...
6
by: MLM450 | last post by:
Is it possible to retrieve an item's "sizeconst" value dynamically? I have the following member defined in a class: private char units; I want the code that values the units member to know...
0
bmallett
by: bmallett | last post by:
First off, i would like to thank everyone for any and all help with this. That being said, I am having a problem retrieving/posting my dynamic form data. I have a form that has multiple options...
4
mehj123
by: mehj123 | last post by:
Hi... I have written some cgi scripts and I need to pass the user id and password between them.. There is a Login page which passes the user name and password to another cgi script through the url...
9
ADezii
by: ADezii | last post by:
One question which pops up frequently here at TheScripts is: 'How do I retrieve data from a Recordset once I've created it?' One very efficient, and not that often used approach, is the GetRows()...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.