Python:替换模块类中的函数 [英] Python: replacing a function within a class of a module

查看:73
本文介绍了Python:替换模块类中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图替换类中定义的函数,以便在不更改实际代码的情况下修改其功能(如内部工作方式)。
我以前从未做过,因此在更换它时遇到了一些问题。
更改代码后,我将无法访问python库中的软件包。

I'm trying to replace a function defined within a class in order to modify its function (as in inner workings) without changing the actual code. I've never done this before, hence, having some problems while replacing it. Changing the code will have me accessing the package within my python library which is not much of an option.

例如,如果模块名为testMOD

For example if the module was called testMOD

class testMOD(object):
      def testFunc(self, variable):
          var = variable
          self.something = var + 12

然后我将导入testMOD,定义一个类(mytest = testMOD ()),然后在类testFunc中访问已定义的函数,并将其更改为已定义的函数。

Then I would import testMOD, define a class (mytest = testMOD()), and access the defined function within the class, testFunc, and change it to already defined function.

例如,

from somemodule import testMOD
mytest = testMOD()

def alternativeFunc(self, variable):
    var = variable
    self.something = var + 1.2

#problem here
mytest.testFunc = alternativeFunc

如您所见,如果我只是用定义的函数手动覆盖(?)类中的函数,它将无法正常工作。
它没有给出任何语法错误,但是,问题是被替换的函数认为'self'是该函数的另一个变量,并说它需要'variable'变量的另一个参数(我想

As you can see, if I just manually overwrite(?) the function in the class with my defined function it won't work properly. It doesn't give any syntax errors, however, the problem is that the replaced function thinks that the 'self' is another variable for the function and says that it requires another argument for the 'variable' variable (I guess that wasn't a good name).

我想要做的是使替换函数与被替换函数完全相同,但是要附加代码或一些小的修改。但是,自我在课堂上几乎没有发挥作用。
是否有办法正确实现已定义的函数来替换导入的类的函数?

What I want to do is to make the replacing function to be exactly the same thing as the replaced function, but with additional code or some minor modifications. However, the 'self' is pretty much not working as it should be in a class. Would there be a way to properly implement a defined function to replace a function of an imported class?

推荐答案

I建议4种解决方案,从最坏到最好(IMHO),但当然也取决于您的特定约束:

I suggest 4 solutions, from the worst to the best (IMHO), but of course it also depends on your specific constraints:


  1. 替换实例方法(1):我使用函数是Python中的描述符这一事实,以便可以在 AlternativeFunc __ get __ 方法。 code>将其作为实例 mytest 的方法获取,并覆盖实例 testFunc 的方法 mytest (不覆盖类方法):

  1. Replace the instance method (1): I use the fact that functions are descriptors in Python, so that I can use the __get__ method on AlternativeFunc to get it as a method of the instance mytest and overwrite the testFunc method of the instance mytest (without overwriting the class method):

class testMOD(object):
    def testFunc(self, variable):
        var = variable
        self.something = var + 12
        print('Original:', self.something)

def alternativeFunc1(self, variable):
    var = variable
    self.something = var + 1.2
    print('Alternative1:', self.something)

mytest1 = testMOD()
mytest1.testFunc(10)   # Original: 22

mytest1.testFunc = alternativeFunc1.__get__(mytest1, testMOD)
mytest1.testFunc(10)   # Alternative1: 11.2
mytestX = testMOD()
mytestX.testFunc(10)   # Original: 22


  • 替换实例方法(2):这次,我使用 types.MethodType 比第一个解决方案更具可读性:

  • Replace the instance method (2): This time, I use types.MethodType which is a bit more readable than the first solution:

    import types
    
    class testMOD(object):
        def testFunc(self, variable):
            var = variable
            self.something = var + 12
            print('Original:', self.something)
    
    def alternativeFunc1(self, variable):
        var = variable
        self.something = var + 1.2
        print('Alternative1:', self.something)
    
    mytest1 = testMOD()
    mytest1.testFunc(10)   # Original: 22
    
    funcType = types.MethodType
    mytest1.testFunc = funcType(alternativeFunc1, mytest1)
    mytest1.testFunc(10)   # Alternative1: 11.2
    mytestX = testMOD()
    mytestX.testFunc(10)   # Original: 22
    


  • 对类方法执行猴子修补。与第一种方法不同,它更改了该类任何实例的行为:

  • Perform a monkey patching of the class method. Differently from the first method, it changes the behavior of any instance of the class:

    class testMOD(object):
        def testFunc(self, variable):
            var = variable
            self.something = var + 12
            print('Original:', self.something)
    
    def alternativeFunc2(self, variable):
        var = variable
        self.something = var + 1.2
        print('Alternative2:', self.something)
    
    mytest2 = testMOD()
    mytest2.testFunc(10)   # Original: 22
    
    testMOD.testFunc = alternativeFunc2
    mytest2.testFunc(10)   # Alternative2: 11.2
    mytestX = testMOD()
    mytestX.testFunc(10)   # Alternative2: 11.2
    


  • 创建从 testMOD 继承的类以覆盖该方法:

  • Create a class inherited from testMOD to override the method:

    class testMODNew(testMOD):
         def testFunc(self, variable):
             var = variable
             self.something = var + 1.2
             print('Alternative3:', self.something)
    
    mytest3 = testMODNew()
    mytest3.testFunc(10) # Alternative3: 11.2
    


  • 这篇关于Python:替换模块类中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆