-1

I have a testmethod in the GParent class which is inherited by Parent and Child..

How can I do that?

I tried this but its not working...

GParent.testmethod(self) 

class GParent():
    def testmethod(self):
        print "This is test method"


class Parent():
    def testmethod(self):
        print "This is test method"


class Child(Parent):
    def __init__(self):
        print "This is init method"
        GParent.testmethod(self)

c = Child() 
Two-Bit Alchemist
  • 17,966
  • 6
  • 47
  • 82
user1050619
  • 19,822
  • 85
  • 237
  • 413
  • 3
    Your `Parent` and `Child` are not inheriting from `GParent`. Did you intend that? – BrenBarn Aug 14 '15 at 19:48
  • possible duplicate of [Call a parent class's method from child class in Python?](http://stackoverflow.com/questions/805066/call-a-parent-classs-method-from-child-class-in-python) – Two-Bit Alchemist Aug 14 '15 at 19:50

2 Answers2

1

first of all: https://docs.python.org/2/tutorial/classes.html#inheritance

At any rate...

GParent.testmethod(self) <-- calling a method before it is defined

class GParent(): <-- always inherit object on your base class to ensure you are using new style classes
    def testmethod(self):
        print "This is test method"


class Parent(): <-- not inheriting anything
    def testmethod(self): <-- if you were inheriting GParent you would be overriding the method that is defined in GParent here.
        print "This is test method"


class Child(Parent):
    def __init__(self):
        print "This is init method"
        GParent.testmethod(self) <-- if you want to call the method you are inheriting you would use self.testmethod()

c = Child()

Take a look at this code and run it, maybe it will help you out.

from __future__ import print_function #so we can use python 3 print function

class GParent(object):
    def gparent_testmethod(self):
        print("Grandparent test method ")


class Parent(GParent):
    def parent_testmethod(self): # 
        print("Parent test method")


class Child(Parent):
    def child_testmethod(self):
        print("This is the child test method")

c = Child()
c.gparent_testmethod()
c.parent_testmethod()
c.child_testmethod()
Alex Jadczak
  • 550
  • 3
  • 11
0

You cannot call GParent's testmethod without an instance of GParent as its first argument.

Inheritance

class GParent(object):
    def testmethod(self):
        print "I'm a grandpa"

class Parent(GParent):

    # implicitly inherit __init__()                                                                                                                  

    # inherit and override testmethod()                                                                                                              
    def testmethod(self):
        print "I'm a papa"

class Child(Parent):

    def __init__(self):
        super(Child, self).__init__()
        # You can only call testmethod with an instance of Child
        # though technically it is calling the parent's up the chain                                                                                      
        self.testmethod()

    # inherit parent's testmethod implicitly

c = Child() # print "I'm a papa"

However, two ways of calling a parent's method explicitly is through composition or class method

Composition

class Parent(object):
    def testmethod(self):
        print "I'm a papa"

class Child(object):
    def __init__(self):
        self.parent = Parent()

        # call own's testmethod                                                                                                                      
        self.testmethod()

        # call parent's method                                                                                                                       
        self.parentmethod()

    def parentmethod(self):
        self.parent.testmethod()

    def testmethod(self):
        print "I'm a son"

c = Child()

Class method

class Parent(object):
    @classmethod
    def testmethod(cls):
        print "I'm a papa"

class Child(object):
    def __init__(self):
        # call own's testmethod                                                                                                                      
        self.testmethod()

        # call parent's method                                                                                                                       
        Parent.testmethod()

    def testmethod(self):
        print "I'm a son"

c = Child()

It has become advisory to use composition when dealing with multiple inheritance, since inheritance creates dependency to the parent class.

Pandemonium
  • 7,724
  • 3
  • 32
  • 51