A decorator @wrapper
is using the wrapt
library to access the wrapped function's class to get the class's name. Using it on Animal.method()
and foo()
works as expected.
Problem: However, the method Animal.classy
decorated with @classmethod
is giving type
as its class name, and the method Animal.static
decorated with @staticmethod
is unable to retrieve its class.
Is it possible for @wrapper
decorator function to obtain the class name Animal
for Animal.classy()
and Animal.static()
?
Expected Output
foo
Animal.method
Animal.classy
Animal.static
Obtained Output
foo
Animal.method
type.classy
static
Code to reproduce
import wrapt
import time
@wrapt.decorator
def wrapper(wrapped, instance, args, kw):
if instance is not None:
print(f'\n{instance.__class__.__name__}.{wrapped.__name__}')
else:
print('\n' + wrapped.__name__)
result = wrapped(*args, **kw)
return result
@wrapper
def foo():
time.sleep(0.1)
class Animal:
@wrapper
def method(self):
time.sleep(0.1)
@wrapper
@classmethod
def classy(cls):
time.sleep(0.1)
@wrapper
@staticmethod
def static():
time.sleep(0.1)
if __name__ == '__main__':
foo() # foo
Animal().method() # Animal.method
Animal.classy() # type.classy
Animal.static() # static