0

I have this Python code:

class Parent(object):
    def __init__(self):
        self.value = 5

    def get_value(self):
        return self.value

Now I must modify the get_value method, and I did this:

class Child(Parent):
    def get_value(self):
        return self.value + 1

As expected, I get this result:

>>> p = Parent()
>>> p.get_value()
5

>>> c = Child()
>>> c.get_value()
6

But I need to get this result:

>>> p = Parent()
>>> p.get_value()
6

In other words, I need to modify the get_value method of Parent class without touching the source code. I've been programming such a long time only in Odoo framework (made with Python 2.7) that I've forgotten things like this, may be this question is duplicated, but I still haven't found a solution.

Can anyone help me, please?

7
  • 1
    Why can't you modify the parent method directly? Commented Feb 28, 2018 at 17:14
  • 1
    Why do you need a Parent object if you don't need the behavior of a Parent object? Why do you need this? There is probably a better solution. Commented Feb 28, 2018 at 17:19
  • @Olivier that's because I'm programming in Odoo and the parent class is declared in other module made by other people. I can't touch the source code of other modules because it's a rule in the Odoo Community, to keep the modular structure. Usually you have to do this inheritance using Odoo framework, and you can do it easily, but this time the code I need to modify is programmed in pure Python (which is uncommon in Odoo), and I need to apply my changes as if I was using Odoo framework. Commented Feb 28, 2018 at 17:21
  • 1
    Next question is then... Why can't you use the Child class if it has the desired behaviour? Commented Feb 28, 2018 at 17:23
  • 1
    Then the short answer is you can't. You cannot change the behaviour of some code without changing the code. Commented Feb 28, 2018 at 17:30

1 Answer 1

3

Perhaps this is what you're looking for:

class MyClass:
  def MyFunc(self):
    return 3

def newFunc(self):
    return 4

# set the function equal to the new function
MyClass.MyFunc = newFunc

b = MyClass()

# normally would print 3, but prints 4 due to newFunc(self)
print(b.MyFunc())

I'm not really sure why you would want to do this though. If someone else works on a part of your code and needs to access MyClass.MyFunc() without noticing you've changed it's behavior, it will give them unexpected results.

It may be better to write a child class that does what you want it to do instead.

Sign up to request clarification or add additional context in comments.

2 Comments

Isn't this basically monkey patching?
I'd love to modify the source code, but if so I'd be breaking the modular structure of Odoo and their rules. I've just given a try to this solution and it's working. And may be applying this patch is horrible, but this is by the moment the only way to achieve what I need. So thank you very much @TreytenCarey!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.