0

In my example C1 class and C2 class both inherit from Base class. In the Base::run(self) method I am calling self.__run1 and self.__run2 methods that are overloaded in C1 and C2.

I can't see a way to execute the C1::__run1 and C2::__run2 methods.

class Base(object):
    def __run1(self):
        pass

    def __run2(self):
        pass

    def run(self):
        self.__run1()
        self.__run2()


class C1(Base):
    def __run1(self):
        print("C1 run1")

class C2(Base):
    def __run2(self):
        print("C2 run2")

o1 = C1()
o2 = C2()

o1.run()
#how to make it return C1 run1?
o2.run()
#how to make it return C2 run2?
Blckknght
  • 100,903
  • 11
  • 120
  • 169
lsa77
  • 15
  • 1
  • 3

1 Answers1

1

The __methods are private. The _methods are hidden and protected. Use _run1() and _run2()

Example

class Base(object):
    def _run1(self): # protected
        pass

    def __run2(self): # private
        pass

    def run(self):
        self._run1()
        self.__run2()


class C1(Base):
    def _run1(self): # overrides old _run1
        print("C1 run1")

class C2(Base):
    def __run2(self): # declares new __run2
        print("C2 run2")

o1 = C1()
o2 = C2()

o1.run()
o2.run()

This will result in:

C1 run1
Peter Zaitcev
  • 316
  • 1
  • 14