0
class Base():
    def __init__(self):
        print("test")

    def first_func(self):
        print("first function call")


class Deriv(Base):
    def __init__(self):
        Base.__init__(self)

    def first_func(self):
        print("Test Derived")

C = Deriv()
C.first_func() # It gives output "Test Derived"

How can I call a first_func() method (output should be "first function call") from base class ( Class Base) using only object C and python 2.7 ?

Aquarthur
  • 609
  • 5
  • 20
Rachit
  • 1
  • 1
  • 1
    You are currently doing something called "overriding" a method. Check this [article](http://blog.thedigitalcatonline.com/blog/2014/05/19/method-overriding-in-python/) out for more information about it! – Aquarthur Oct 03 '18 at 09:29

1 Answers1

0

You can use Base.first_func(C) to explicitly call the base class function:

class Base():
    def __init__(self):
        print("test")

    def first_func(self):
        print("first function call")


class Deriv(Base):
    def __init__(self):
        Base.__init__(self)

    def first_func(self):
        print("Test Derived")

C = Deriv()
Base.first_func(C) 

Output is:

test
first function call

If you were using new-style classes (ie., you had derived Base from object), using super is preferred, as mentioned in here.

rainer
  • 6,769
  • 3
  • 23
  • 37