#!/usr/bin/env python3 # -*- coding:utf-8 -*- from operator import methodcaller # 装饰类方法的装饰器,并可以通过传递类方法.功能:在被修饰的方法之前调用新增方法! def doBefore(funName): def wrapper(fun): def inner(*args, **kwargs): cls = args[0] methodcaller(funName)(cls) return fun(*args, **kwargs) return inner return wrapper class A(): @doBefore(funName='bb') @doBefore(funName='cc') def aa(self, x, y): c = x + y print('{}+{}={}'.format(x, y, c)) def bb(self): print('do other something!') def cc(self): print('do other something slse!') if __name__ == '__main__': a = A() a.aa(3, 5)