python使用UnboundMethodType修改类方法

from types import UnboundMethodType


class class1(object):
    def fun1(self):
        print 'fun1'


oldfun1 = class1.fun1


def fun1new(self):
    print 'fun1new'
    return oldfun1(self)


class1.fun1 = UnboundMethodType(fun1new, None, class1)

class1().fun1()

最终输出

fun1new
fun1

 

posted @ 2018-04-02 15:32  半块西瓜皮  阅读(348)  评论(0编辑  收藏  举报