Printing variable names

M

Mike

mylist = [a, b, c]

I want to print out the names of the variables in mylist (not the
values of a, b, and c). How do I go about doing this. Thanks.

Mike
 
M

Mark McEahern

Mike said:
mylist = [a, b, c]

I want to print out the names of the variables in mylist (not the
values of a, b, and c). How do I go about doing this. Thanks.

Mike
There's no simple answer to this. Consider the fact that you can have
more than one name bound to any given mutable instance. What if:

a = range(10)
b = a

mylist = [a]

what name do you want printed for the first item in mylist--'a' or 'b'?

One idea is to use a dictionary instead. Then:

for key, value in mydict.iteritems():
print '%(key)s = %(value)s' % locals()

I'm curious what problem you're trying to solve.

Cheers,

// m
 
N

Nuff Said

mylist = [a, b, c]

I want to print out the names of the variables in mylist (not the
values of a, b, and c). How do I go about doing this. Thanks.

The following example shows that this does not really make sense:

a = 1; b = 2; c = 3;
mylist = [a, b, c]
a = 4; b = 5; c = 6;
print mylist
print a, b, c

Result:
[1, 2, 3]
4 5 6

and *not*:
[4, 5, 6]
4 5 6

You might want to google for 'Python object reference' etc.
Moreover, having a look (e.g. in the tutorial) at how Python
passes arguments to functions (mutable and immutable objects)
might help to get a better understanding of what is going on
behind the scenes.

HTH / Nuff
 
D

Dan Bishop

Mark McEahern said:
Mike said:
mylist = [a, b, c]

I want to print out the names of the variables in mylist (not the
values of a, b, and c). How do I go about doing this. Thanks.

There's no simple answer to this. Consider the fact that you can have
more than one name bound to any given mutable instance.

Why did you specify "mutable"? The same applies to immutable
instances.
I'm curious what problem you're trying to solve.

So am I. The question shows up here all the time, but I've never seen
a reason for it.
 
S

Scott David Daniels

Mark said:
Mike said:
mylist = [a, b, c]

I want to print out the names of the variables in mylist (not the
values of a, b, and c). How do I go about doing this. Thanks.
Mike
... One idea is to use a dictionary instead. Then:
for key, value in mydict.iteritems():
print '%(key)s = %(value)s' % locals()
I'm curious what problem you're trying to solve.

Mark's questions are very much on target. If the purpose is debugging,
you might be satisfied with something like:

def vnames(value, *dicts):
"""From a value and some dictionaries and give names for the
value"""
result = []
for d in dicts:
result.extend([key for key, val in d.iteritems()
if val is value])
result.append(repr(value))
return result

Which you might use like:

a,b,c = 1,2,3
d,e,f = 5,4,3

for v in range(10):
print v, vnames(v, locals(), globals())

Note: You probably need only locals or globals if you are at the top
level of an interpreter such as Idle or the python shell.
You might prefer '==' to 'is', but remember that 0.0 == 0 == 0L.

-Scott David Daniels
(e-mail address removed)
 
M

Mike

Thanks for the info. That does clear up a few things for me.

This is what I'm trying to accomplish:

Basically I have a list of pointers to functions (or whaterver it's called in
Python). Something like this:

commands = [func1, func2, ...funcN]

This is in a script that I use to test an embedded system through the comport.
I call the script with the command number (func1 etc...), which calls the
corresponding function, which sends a command to the embedded system.

I'd like to be able to call the script with --help and have it spit out
the list of commands (the names func1, func2 etc...).


Mike




Mark McEahern said:
Mike said:
mylist = [a, b, c]

I want to print out the names of the variables in mylist (not the
values of a, b, and c). How do I go about doing this. Thanks.

Mike
There's no simple answer to this. Consider the fact that you can have
more than one name bound to any given mutable instance. What if:

a = range(10)
b = a

mylist = [a]

what name do you want printed for the first item in mylist--'a' or 'b'?

One idea is to use a dictionary instead. Then:

for key, value in mydict.iteritems():
print '%(key)s = %(value)s' % locals()

I'm curious what problem you're trying to solve.

Cheers,

// m
 
M

Mark McEahern

[Me]
There's no simple answer to this. Consider the fact that you can have
more than one name bound to any given mutable instance.

[Dan Bishop]
Why did you specify "mutable"? The same applies to immutable
instances.

I didn't think through the immutable issue, that's all. So, in short,
no good reason. <wink>

Cheers,

// m
 
P

Peter Otten

Mike said:
This is what I'm trying to accomplish:

Basically I have a list of pointers to functions (or whaterver it's called
in
Python). Something like this:

commands = [func1, func2, ...funcN]

This is in a script that I use to test an embedded system through the
comport. I call the script with the command number (func1 etc...), which
calls the corresponding function, which sends a command to the embedded
system.

I'd like to be able to call the script with --help and have it spit out
the list of commands (the names func1, func2 etc...).

You're lucky, functions "know" their name:
def func1(): pass ....
def func2(): pass ....
for f in [func1, func2]:
.... print f.__name__
....
func1
func2

Peter
 

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,175
Messages
2,570,946
Members
47,498
Latest member
yelene6679

Latest Threads

Top