the name of a method
Matimus
mccredie at gmail.com
Thu Jan 15 13:22:29 EST 2009
On Jan 15, 8:23 am, thomas.steffe... at googlemail.com wrote:
> Hello,
>
> I have a Class:
>
> class myClass:
> def __init__(self):
> # do something
> print "name of class = " + self.__class__.__name__
>
> def myMethod(self):
> # do something
> print "name of method = " + "myMethod"
> return
>
> ...
>
> I print the name of the class with self.__class__.__name__ in
> __init__.
> I want to print also in every method of myClass the name of the
> method.
> How can I get the name? I would not like to write e.g. "myMethod". Is
> there a variable like self.__class__.__name__ for this?
> Thanks for your hints, Thomas
I would just use a decorator:
>>> def print_method_name(meth):
... def new_meth(*args, **kwargs):
... print meth.func_name
... return meth(*args, **kwargs)
... return new_meth
...
>>> class MyClass(object):
... @print_method_name
... def my_method(self):
... pass
...
>>> x = MyClass()
>>> x.my_method()
my_method
Matt
More information about the Python-list
mailing list