3 回答

TA贡献1848条经验 获得超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条经验 获得超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)
添加回答
举报