How to extend Class instance

前端 未结 4 1213
礼貌的吻别
礼貌的吻别 2021-01-20 06:50

MyClass is defined in module.py. There is no way we can modify it. But we do know the Class definition looks like this:

class MyCla         


        
4条回答
  •  悲哀的现实
    2021-01-20 07:16

    Ended up with this solution till there is better one posted...

    class MyClass:
        def method(self, msg):
            print 'from method:', msg
    
    def function(msg, callback):
        print 'from function:', msg
        callback(msg)
    
    foo = MyClass()
    foo.function = function
    foo.function(msg='message', callback=foo.method)
    

提交回复
热议问题