Like java, without modifying the source code, conduct pre- and post-function method to add a strong
class Old(): def do_sth(self): print('do something....') class MiddleWare(): def __init__(self,old): self.old = old DEF __call__ (Self, * args, ** kwargs): Print ( ' do something before the Old # do_sth method, the pre-enhancement ' ) RET = self.old (* args, ** kwargs) Print ( ' do something after the Old # do_sth method, the pre-enhancement ' ) return RET if __name__ == '__main__': old1= Old() old1.do_sth() Print ( ' --------------- adding a strong function of the method MiddleWare do_sth ------------------- class ' ) old= Old() old.do_sth = MiddleWare(old.do_sth) old.do_sth()