You can't, not with __init__
. Once __init__
is being called, the instance is already created.
You want a factory function instead:
class AorB: pass
class A(AorB):
def __init__(self,par):
self.par=par
class B(AorB):
def __init__(self):
pass
def AorB(par):
return A(par) if par else B()
From an API point of view, there is no difference; AorB
is a callable that produces either an A()
or a B()
instance.
The other possible route involves defining a __new__
function instead; this is the class constructor:
class AorB:
def __new__(cls, par=None):
if cls is not AorB: return super().__new__(cls)
return super().__new__(A) if par else super().__new__(B)
class A(AorB):
def __init__(self, par):
self.par = par
class B(AorB):
def __init__(self, par=None):
pass
Which is just a more involved factory function, really. Note that the __new__
method returns the result of super().__new__
of one of the subclasses based on par
, so both A
and B
will be passed a par
parameter, wether they want one or not.
The if cls is not AorB
line is needed to allow instantiating A()
or B()
directly; you can omit that line if that is not a requirement and only the AorB
factory class is used.