Например:
Код: Выделить всё
import functools
def wrap(f):
@functools.wraps(f)
def wrapped(*args, **kwargs):
print(f"{f.__name__} called with args: {args}, kwargs: {kwargs}")
return f(*args, **kwargs)
return wrapped
class Test:
@staticmethod
def static():
print("hello")
def method(self):
self.static()
Test.static = wrap(Test.static)
Test().method()
Код: Выделить всё
static called with args: (,), kwargs: {}
Traceback (most recent call last):
File "/Users/aiguofer/decorator_example.py", line 20, in
Test().meth()
File "/Users/aiguofer/decorator_example.py", line 16, in meth
self.static()
File "/Users/aiguofer/decorator_example.py", line 7, in wrapped
return f(*args, **kwargs)
TypeError: Test.static() takes 0 positional arguments but 1 was given
Код: Выделить всё
static called with args: (), kwargs: {}
hello
Мне бы хотелось понять, что происходит, а также возможные решения этой проблемы!
Подробнее здесь: https://stackoverflow.com/questions/792 ... es-self-as