Go Back   Velocity Reviews > Newsgroups > Python
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Python - Printing variable names

 
Thread Tools Search this Thread
Old 01-18-2004, 07:22 PM   #1
Default Printing variable names


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


Mike
  Reply With Quote
Old 01-18-2004, 07:39 PM   #2
Mark McEahern
 
Posts: n/a
Default Re: Printing variable names
Mike wrote:

>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



Mark McEahern
  Reply With Quote
Old 01-18-2004, 08:16 PM   #3
Nuff Said
 
Posts: n/a
Default Re: Printing variable names
On Sun, 18 Jan 2004 11:22:08 -0800, Mike wrote:

> 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



Nuff Said
  Reply With Quote
Old 01-18-2004, 10:51 PM   #4
Dan Bishop
 
Posts: n/a
Default Re: Printing variable names
Mark McEahern <> wrote in message news:<mailman.479.1074455535.12720.python->...
> Mike wrote:
>
> >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.


Dan Bishop
  Reply With Quote
Old 01-18-2004, 11:25 PM   #5
Scott David Daniels
 
Posts: n/a
Default Re: Printing variable names
Mark McEahern wrote:
> Mike wrote:
>> 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



Scott David Daniels
  Reply With Quote
Old 01-19-2004, 12:28 AM   #6
Mike
 
Posts: n/a
Default Re: Printing variable names
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 <> wrote in message news:<mailman.479.1074455535.12720.python->...
> Mike wrote:
>
> >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



Mike
  Reply With Quote
Old 01-19-2004, 12:38 AM   #7
Mark McEahern
 
Posts: n/a
Default Re: Printing variable names
[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




Mark McEahern
  Reply With Quote
Old 01-19-2004, 12:41 AM   #8
Peter Otten
 
Posts: n/a
Default Re: Printing variable names
Mike wrote:

> 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


Peter Otten
  Reply With Quote
Old 01-19-2004, 03:22 PM   #9
Mike
 
Posts: n/a
Default Re: Printing variable names
That makes life easy. Problem solved! Thanks all for the help.

Mike



>
> You're lucky, functions "know" their name:
>
> >>> def func1(): pass

> ...
> >>> def func2(): pass

> ...
> >>> for f in [func1, func2]:

> ... print f.__name__
> ...
> func1
> func2
>
> Peter



Mike
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing value with out using variable in query string in PHP! Ali_ggl General Help Related Topics 0 11-29-2008 12:22 PM
Control Printing and Require Authentication for printing with using Java transformer Software 0 07-02-2007 04:33 AM
Variable Scope in asp.Net jansi_rk Software 1 09-18-2006 06:05 PM
What's available in variable speed VCR's ? E. Matthews DVD Video 3 10-20-2005 11:44 AM
I LOVE FULLSCREEN Lookingglass DVD Video 139 01-06-2004 02:13 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46