471,354 Members | 1,472 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 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 1445
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Sven Wynsberghe | last post: by
10 posts views Thread by Girish | last post: by
4 posts views Thread by namanhvu | last post: by
6 posts views Thread by MLM450 | last post: by
mehj123
4 posts views Thread by mehj123 | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.