class parent:
def __new__(cls, *args):
print('parent', i, args)
if args and type(args[0]) == int:
return child1(*args)
return super(parent, cls).__new__(cls)
def __init__(self, A):
self.A = A
class child1(parent):
def __new__(cls, *args):
print('child1', i, args)
if args and type(args[0]) == str:
return parent(*args)
return super(child1, cls).__new__(cls)
def __init__(self, A, B):
self.A = A
self.B = B
def __call__(self):
return self.A, self.B
i = 1
pa = parent('AB')
i = 2
c1 = child1(1, 4)
i = 3
c0 = parent(2, 3)
i = 4
p0 = child1('DC')
print(pa, pa.A)
print(c1, c1())
print(c0, c0())
print(p0, p0.A)
'''
parent 1 ('AB',)
child1 2 (1, 4)
parent 2 ()
parent 3 (2, 3)
child1 3 (2, 3)
parent 3 ()
child1 4 ('DC',)
parent 4 ('DC',)
<__main__.parent object at 0x00000162D6757520> AB
<__main__.child1 object at 0x00000162D6757580> (1, 4)
<__main__.child1 object at 0x00000162D67575E0> (2, 3)
<__main__.parent object at 0x00000162D67576A0> DC
'''