Christian Staudt Christian Staudt - 1 year ago 75
Python Question

Python function decorator should print full method name

The following useful decorator measures the execution time of a function. It also prints the function's name. However, it would be great if it could also print the class name if the function is a method. What is a concise way to get to the full method name a la


import time

def timeit(f):

def timed(*args, **kw):

ts = time.time()
result = f(*args, **kw)
te = time.time()

print('func:%r args:[%r, %r] took: %2.4f sec' % (f.__name__, args, kw, te-ts))
return result

return timed

Answer Source

What about .__qualname__ ?

In [1]: class MyClass(object):
   ...:     def my_method(self):
   ...:         pass

In [2]: MyClass.my_method.__qualname__
Out[2]: 'MyClass.my_method'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download