[Tutor] print method name

Lie Ryan lie.1296 at gmail.com
Thu Dec 8 16:40:18 CET 2011


On 12/09/2011 01:49 AM, rail shafigulin wrote:
> i created a class and in some instances when i use it call some of its
> methods i need to print a method name. the online search did produce
> some results but none of them seem to work for me. for example one of
> them said just to use __name__ or func_name but it didn't work for me.
> i'm using python 3.1.1
>

I'm guessing that you're  doing something like

def foo():
     ...

print foo().__name__

foo() would call the foo() function, so you'd be asking the .__name__ of 
the object returned by foo not the __name__ of foo. Instead you'd need 
to use the the function object itself, IOW don't call foo just do:

print foo.__name__


This should work for both python 2 and python 3, and for both class 
methods or regular functions.



More information about the Tutor mailing list