标签:imp 函数 led 结果 ret cal was cto def
python functools.wraps 实例解读
1. 未使用wraps的实例
#!/usr/bin/env python # coding:utf-8
def logged(func): def with_logging(*args, **kwargs): ‘‘‘i am wraps‘s doc‘‘‘ print func.__name__ + " was called" return func(*args, **kwargs) return with_logging @logged def f(x): """i am original doc""" return x + x * x print f.__name__ # 期望的是原本的名字f,结果换成了装饰器里的新函数的名字with_logging print f.__doc__ # 期望的是原本的注释,加了个装饰器成了新加装饰器里的注释了
2. 使用了wraps的实例
from functools import wraps def logged(func): @wraps(func) def with_logging(*args, **kwargs): ‘‘‘i am wraps‘s doc‘‘‘ print func.__name__ + " was called" return func(*args, **kwargs) return with_logging @logged def f(x): """i am original doc""" return x + x * x print f.__name__ # prints ‘f‘ print f.__doc__ # prints ‘i am original doc‘
标签:imp 函数 led 结果 ret cal was cto def
原文地址:http://www.cnblogs.com/aaron-agu/p/7763048.html