473,388 Members | 1,346 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,388 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 1542
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.