四川建设厅的网站,备案网站主办者承诺书,东莞 网站建设,小程序api开发函数也是对象要理解Python装饰器#xff0c;首先要明白在Python中#xff0c;函数也是一种对象#xff0c;因此可以把定义函数时的函数名看作是函数对象的一个引用。既然是引用#xff0c;因此可以将函数赋值给一个变量#xff0c;也可以把函数作为一个参数传递或返回。同…函数也是对象要理解Python装饰器首先要明白在Python中函数也是一种对象因此可以把定义函数时的函数名看作是函数对象的一个引用。既然是引用因此可以将函数赋值给一个变量也可以把函数作为一个参数传递或返回。同时函数体中也可以再定义函数。装饰器本质可以通过编写一个纯函数的例子来还原装饰器所要做的事。def decorator(func):def wrap():print(Doing someting before executing func())func()print(Doing someting after executing func())return wrapdef fun_test():print(func)fun_test decorator(fun_test)fun_test()# Output:# Doing someting before executing func()# func# Doing someting after executing func()fun_test所指向的函数的引用传递给decorator()函数decorator()函数中定义了wrap()子函数这个子函数会调用通过func引用传递进来的fun_test()函数并在调用函数的前后做了一些其他的事情decorator()函数返回内部定义的wrap()函数引用fun_test接收decorator()返回的函数引用从而指向了一个新的函数对象通过fun_test()调用新的函数执行wrap()函数的功能从而完成了对fun_test()函数的前后装饰Python中使用装饰器在Python中可以通过符号来方便的使用装饰器功能。def decorator(func):def wrap():print(Doing someting before executing func())func()print(Doing someting after executing func())return wrapdecoratordef fun_test():print(func)fun_test()# Output:# Doing someting before executing func()# func# Doing someting after executing func()装饰的功能已经实现了但是此时执行:print(fun_test.__name__)# Output:# wrapfun_test.__name__已经变成了wrap这是应为wrap()函数已经重写了我们函数的名字和注释文档。此时可以通过functools.wraps来解决这个问题。wraps接受一个函数来进行装饰并加入了复制函数名称、注释文档、参数列表等等功能。这可以让我们在装饰器里面访问在装饰之前的函数的属性。更规范的写法from functools import wrapsdef decorator(func):wraps(func)def wrap():print(Doing someting before executing func())func()print(Doing someting after executing func())return wrapdecoratordef fun_test():print(func)fun_test()print(fun_test.__name__)# Output:# Doing someting before executing func()# func# Doing someting after executing func()# fun_test带参数的装饰器通过返回一个包裹函数的函数可以模仿wraps装饰器构造出一个带参数的装饰器。from functools import wrapsdef loginfo(infoinfo1):def loginfo_decorator(func):wraps(func)def wrap_func(*args, **kwargs):print(func.__name__ was called)print(info: %s % info)return func(*args, **kwargs)return wrap_funcreturn loginfo_decoratorloginfo()def func1():passfunc1()# Output:# func1 was called# info: info1loginfo(infoinfo2)def func2():passfunc2()# Output:# func2 was called# info: info2装饰器类通过编写类的方法也可以实现装饰器并让装饰器具备继承等面向对象中更实用的特性首先编写一个装饰器基类from functools import wrapsclass loginfo:def __init__(self, infoinfo1):self.info infodef __call__(self, func):wrapdef wrap_func(*args, **kwargs):print(func.__name__ was called)print(info: %s % self.info)self.after() # 调用after方法可以在子类中实现return func(*args, **kwargs)return wrap_funcdef after(self):passloginfo(infoinfo2)def func1():pass# Output:# func1 was called# info: info1再通过继承loginfo类扩展装饰器的功能class loginfo_after(loginfo):def __init__(self, info2info2, *args, **kwargs):self.info2 info2super(loginfo_after, self).__init__(*args, **kwargs)def after(self):print(after: %s % self.info2)loginfo_after()def func2():passfunc2()# Output:# func2 was called# info: info1# after: info2以上这篇老生常谈Python进阶之装饰器就是小编分享给大家的全部内容了希望能给大家一个参考也希望大家多多支持我们。本文标题: 老生常谈Python进阶之装饰器本文地址: http://www.cppcns.com/jiaoben/python/190545.html