Printing variable names

Mike mike at odyne.com
Sun Jan 18 19:28:18 EST 2004


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 <mark at mceahern.com> wrote in message news:<mailman.479.1074455535.12720.python-list at python.org>...
> 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



More information about the Python-list mailing list