Printing variable names

Peter Otten __peter__ at web.de
Sun Jan 18 19:41:50 EST 2004


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



More information about the Python-list mailing list