python 装饰器 第八步:使用类来作为装饰器参数

#第八步:使用类作为装饰器参数
#装饰器使用的操作类
class Wish:
    #祈求方法
    def before():
        print('饭前洗洗手')
    #还愿方法
    def after():
        print('饭后溜溜腿')
#装饰器函数
def outer(cls):
    def kuozhan(func):
        # 未来的eat函数
        def neweat():
            # 扩展1(类中存在扩展内容)
            cls.before()
            # 调用基本函数
            func()
            # 扩展2(类中存在扩展内容)
            cls.after()

        return neweat

    return kuozhan

#基本函数
@outer(Wish)#装饰器
def eat():
    print('吃饭')

#调用函数
eat()
原文地址:https://www.cnblogs.com/zhangboblogs/p/8018707.html