Retrieving a variable's name.

R

rodrigo

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
 
G

Gabriel Genellina

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>
 
E

Evan Klitzke

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.
 
P

Paul Rubin

rodrigo said:
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.
 
E

Evan Klitzke

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.
 
J

James Stroud

rodrigo said:
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

Use a dict as that is what you access when you use namespaces anyway:

py> type(globals())
<type 'dict'>

The essential problem with what you suggest is that python assigns names
to references to objects, so the interpreter can not tell what name you
want by a reference to the object, so it gives you whatever name it
happens to find, which, since the names are keys to a dict, come in
arbitrary order. For example, consider this situation:

py> a = 4
py> b = a
py> ns = globals()
py> for avar in a,b:
.... for k,v in ns.items():
.... if v is avar:
.... print '%s is %s' % (k,v)
.... break
....
avar is 4
avar is 4

So getting at the names by reference to thee objects will never be
dependable. Passing names makes more sense:

py> for aname in ['a', 'b']:
.... print '%s is %s' % (aname, globals()[aname])
....
a is 4
b is 4

But this is tantamount to using a dict:

py> mydict = {'a':4, 'b':2}
py> for aname in ['a', 'b']:
.... print '%s is %s' % (aname, mydict[aname])
....
a is 4
b is 2

Which is preferred because it keeps your namespaces cleaner.

James


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 
S

Steve Holden

rodrigo said:
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 -------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top