Код: Выделить всё
>>> def debug(func):
... msg=func.__qualname__
... def wrapper(*args, **kwargs):
... print(msg)
... return func(*args, **kwargs)
... return wrapper
...
>>> @debug
... def add(x, y):
... return x+y
...
>>> add(1,2)
add
3
Код: Выделить всё
>>> from functools import wraps
>>>
>>> def debug(func):
... msg=func.__qualname__
... @wraps
... def wrapper(*args, **kwargs):
... print(msg)
... return func(*args, **kwargs)
... return wrapper
...
>>> @debug
... def add(x, y):
... return x+y
...
>>> add(1,2)
Traceback (most recent call last):
File "", line 1, in
TypeError: update_wrapper() got multiple values for argument 'wrapped'
>>>
Подробнее здесь: https://stackoverflow.com/questions/443 ... -decorator
Мобильная версия