Surya Banerjee
Surya Banerjee

Reputation: 11

How to override the same parent class in 2 classes?

So lets suppose there is this in a library -

class A():
  ....

class B(A):
  ....

class C(A):
  ....

Is there any way to override the __init__() in class A before it is inherited by the child classes. So something like this

import A as _A

#Then override the __init__()
class A(_A):
  def _init__():
     .....
     super().__init__()

Now I want this class A to be used for inheritance in child classes B and C and not the original A.

Upvotes: 0

Views: 54

Answers (1)

Michael Butscher
Michael Butscher

Reputation: 10959

You can replace functions of a class by others (usually not recommended). E. g. in a separate module:

import A.A as _A

old_init = _A.__init__

def new_init(self):
    .....
    old_init(self)

_A.__init__ = new_init

Upvotes: 1

Related Questions