3 回答

TA貢獻1848條經(jīng)驗 獲得超6個贊
def logged(func): def with_logging(*args, **kwargs): print(func.__name__ + " was called") return func(*args, **kwargs) return with_logging
@loggeddef f(x): """does some math""" return x + x * x
def f(x): """does some math""" return x + x * x f = logged(f)
f
print(f.__name__)
with_logging
f
with_logging
x
*args
**kwargs
functools.wraps
wraps
from functools import wrapsdef logged(func): @wraps(func) def with_logging(*args, **kwargs): print(func.__name__ + " was called") return func(*args, **kwargs) return with_logging@loggeddef f(x): """does some math""" return x + x * xprint(f.__name__) # prints 'f'print(f.__doc__) # prints 'does some math'

TA貢獻1828條經(jīng)驗 獲得超3個贊
__name__
__name__
class DecBase(object): func = None def __init__(self, func): self.__func = func def __getattribute__(self, name): if name == "func": return super(DecBase, self).__getattribute__(name) return self.func.__getattribute__(name) def __setattr__(self, name, value): if name == "func": return super(DecBase, self).__setattr__(name, value) return self.func.__setattr__(name, value)
class process_login(DecBase): def __call__(self, *args): if len(args) != 2: raise Exception("You can only specify two arguments") return self.func(*args)
添加回答
舉報