标签:rds span 默认 with logs pre order keyword call
functools模块:管理函数的工具
import functools # 原函数 def myfunc(a, b=2): """Docstring for myfunc().""" print(‘ called myfunc with:‘, a, b) return # 输出函数 def show_details(name, f, is_partial=False): print(name) print(f) if not is_partial: print(f.__name__) if is_partial: print(f.func) print(f.args) print(f.keywords) return # show_details(‘myfunc‘, myfunc) # myfunc(‘a‘, 3) p1 = functools.partial(myfunc, b=4) # 包装后的函数 show_details(‘partial with named‘, p1, True) p1(‘passing a‘) # 调用包装后的函数 p1(‘override b‘, b=5)
复制原函数的属性到partial对象
functools.update_wrapper(p1, myfunc) # 注释掉这一行,运行报错
print(p1.__name__)
print(p1.__doc__)
包装实例对象的函数
class MyClass(object): def method(self, a, b=2): """Docstring for method().""" print(‘ called method with:‘, a, b) return o = MyClass() p1 = functools.partial(o.method, b=4) show_details(‘partial with named‘, p1, True) functools.update_wrapper(p1, o.method) print(p1.__name__) print(p1.__doc__)
@functools.wraps 装饰器
def simple_decorator(f): @functools.wraps(f) def decorated(a=‘decorated‘, b=1): print(a, b) f(a, b=b) return return decorated wrapped = simple_decorator(myfunc) print(wrapped.__name__) print(wrapped.__doc__)
为类添加富比较方法,类必须提供__eq__和另一个富比较方法
标签:rds span 默认 with logs pre order keyword call
原文地址:http://www.cnblogs.com/wj5633/p/7043956.html