py-装饰器

 

 首先执行log('execute'),返回的是decorator函数,再调用返回的函数,参数是now函数,返回值最终是wrapper函数

 

 

 

在面向对象(OOP)的设计模式中,decorator被称为装饰模式。

OOP的装饰模式需要通过继承和组合来实现,而Python除了能支持OOP的decorator外,直接从语法层次支持decorator。

Python的decorator可以用函数实现,也可以用类实现。

decorator可以增强函数的功能,定义起来虽然有点复杂,但使用起来非常灵活和方便。

 

 

猜你喜欢

转载自blog.csdn.net/qq_35355153/article/details/81511448