0

I have a problem in inheritance with python.

I need to override a python function and whenever that class called it should call the child method not the parent.

Here is the code : in file called test_inherit.py

class test_me(object):

    def get_uid(self, node):
        return uid

and what i did is : in file called test_new.py

from test_inherit import  test_me
class test_new(test_me)
     def get_uid(self,node):
         print "X is here",node
         return uid

i need that when i call test_me class or test_new class , it should call the get_uid of test_new class but i don't know how to achieve that ?!

It's simple .. i have a project that has this class "test_me" which contain a function called get_uid ,, and that fuction is called in the project many times,i want to call the function of the child when ever the parent function is called because i don't want to change the main project ,, i want to do my edits in separate file .

  • How are you calling the code? What is indicating to you that it is not working? – idjaw Nov 05 '16 at 18:46
  • That does not work that way. If you have a `test_me` instance, it is not going to use methods from `test_new` class. – zvone Nov 05 '16 at 18:47
  • when ever i call the get_uid i didn't get the print function, and in the project the test_inherit is already there and called by (get_inherit.get_uid) but i need to replace the function get_uid without replace anything in the main project –  Nov 05 '16 at 18:48
  • 1
    @MostafaMohamed Edit your question and show *exactly* what it is you are doing that is giving you problems, please. – idjaw Nov 05 '16 at 18:49
  • @ zvone you r telling me that i can't override that class without changing it, i can't set what should be called –  Nov 05 '16 at 18:50
  • @MostafaMohamed Once again. Edit your question and show exactly what it is you are doing in your code, so we know *exactly* what the problem is. Sometimes what might seem clear to you in your explanation is not clear to others. – idjaw Nov 05 '16 at 18:52
  • It's simple guys .. i have a project that has this class "test_me" which contain a function called get_uid ,, and that fuction is called in the project many times,i want to call the child function of the child when ever the parent function is called. –  Nov 05 '16 at 18:53
  • I did @ idjaw is it now clear ? –  Nov 05 '16 at 18:54
  • So, you want to create an instance of `test_me` but call the method of `test_me` from class `test_new`? That seems like a big [XY](http://xyproblem.info/) problem. – idjaw Nov 05 '16 at 18:59
  • @MostafaMohamed *(Trying to guess what you are asking)*: When you define `test_new` class, you are just defining a new class. You are not modifying the `test_me` class in any way. – zvone Nov 05 '16 at 19:00
  • Nope , Simply i want when ever parent class function 'get_uid' called , it should also call the 'get_uid' of the child or just call the child function –  Nov 05 '16 at 19:02
  • test_me.get_uid() --->"X is here" –  Nov 05 '16 at 19:03
  • Possible duplicate of [Python using derived class's method in parent class?](http://stackoverflow.com/questions/2297843/python-using-derived-classs-method-in-parent-class) – idjaw Nov 05 '16 at 19:04
  • 1
    We are programmers... talk to us in code! When I fix a few bugs in your example so that it works, and then do `test_new().get_uid('some node')` the child method is called. Can you rework your code to be a working example and include test code that shows the problem? – tdelaney Nov 05 '16 at 19:10
  • You can't do this in general... suppose there are multiple child classes, who gets called? You can monkey-patch the parent class (I'll give an example) but if you have control of the source code for these classes there may be a better way of handling the problem. – tdelaney Nov 05 '16 at 19:22

1 Answers1

0

Subclassing changes the behavior of the child class but that has no effect on the parent. A class may have many inheritors and it wouldn't be clear which subclass should be called even if you could.

You can monkey-patch the parent as in the following example. I camel-cased the class name to help is stand out from variable names. The risk is that code called before the patching gets the old uid method.

class TestMe(object):

    def get_uid(self, node):
        return "parent"

test1 = TestMe()
uid1 = test1.get_uid()

# monkey-patch to modify parent class
def _my_get_uid(self, node):
    print "Parent hacked", node
    return "hack"
TestMe.get_uid = _my_get_uid

test2 = TestMe()

assert uid1 == "parent", "cant change old calls"
assert test1.get_uid(1) == "hack", "existing objects changed"
assert test2.get_uid(1) == "hack", "new objects changed"
tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • I liked your answer and i have and you give me idea of how to solve my problem. Thanks –  Nov 05 '16 at 21:10