OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Python: replacing a function within a class of a module

  • Thread starter Thread starter Cody Chung
  • Start date Start date
C

Cody Chung

Guest
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, and, hence, am 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.

For example, if the module was called testMOD,

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

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.

For instance,

Code:
from somemodule import testMOD
mytest = testMOD()

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

# A problem here
mytest.testFunc = alternativeFunc

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).

I want 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?
<p>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, and, hence, am having some problems while replacing it.</p>
<p>Changing the code will have me accessing the package within my Python library which is not much of an option.</p>
<p>For example, if the module was called testMOD,</p>
<pre><code>class testMOD(object):
def testFunc(self, variable):
var = variable
self.something = var + 12
</code></pre>
<p>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.</p>
<p>For instance,</p>
<pre><code>from somemodule import testMOD
mytest = testMOD()

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

# A problem here
mytest.testFunc = alternativeFunc
</code></pre>
<p>As you can see, if I just manually overwrite(?) the function in the class with my defined function it won't work properly.</p>
<p>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).</p>
<p>I want 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.</p>
<p>Would there be a way to properly implement a defined function to replace a function of an imported class?</p>
 

Latest posts

I
Replies
0
Views
1
impact christian
I
Top