Refer to function name (newbie ques)

Alex Martelli aleax at aleax.it
Thu Jul 25 12:23:57 EDT 2002


Pete Shinners wrote:

> chris ciotti wrote:
>> Is there a way to get the name of the running function?  In a  script
>> I'm doing some error checking and I want to be able to name the
>> function that screws up.  Something like:
> 
> one problem is if the function has multiple names...
> 
>      def foo():
>          print 'In Foo'
>      bar = foo

Not a real problem: bar.__name__ is 'foo'.  The function may
have any number of references to it, and may be called from
any reference including one without a name, e.g.

        alist = []
        alist.append(foo)
        alist[-1]()

but the name of the FUNCTION, quite independently from the
name (or lack thereof) of any REFERENCE to the function,
is still 'foo'.


Classes, functions and modules are object with names (in
attribute __name__).  Other kinds aren't, of course.



Alex




More information about the Python-list mailing list