#第八步:使用类作为装饰器参数
#装饰器使用的操作类
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()

 

相关文章:

  • 2021-05-31
  • 2022-12-23
  • 2022-12-23
  • 2021-06-06
  • 2021-07-29
  • 2021-08-20
  • 2021-05-29
  • 2022-12-23
猜你喜欢
  • 2022-01-17
  • 2021-09-18
  • 2022-01-18
  • 2022-12-23
  • 2022-01-27
  • 2020-05-11
  • 2022-12-23
相关资源
相似解决方案