I have a base class which has a __init__ that takes two parameters. I want to create a derived class object, given that I have a base class object.
I want to do the following,
a_obj = A(10, 'f')
## given that I have a_obj, create b_obj
b_obj = B(a_obj)
and get a result such that
type(b_obj) => B
print(b_obj.age) => 10
print(b_obj.sex) => "f"
I tried the following, where the __new__ of B would call A's __new__ and ask it to create an object of type B(an empty object). But at this point I dont know how to set B to have all the functionalities of A.
class A:
def __init__(self, age, sex):
self.age = age
self.sex = sex
class B(A):
def __new__(cls, a_obj):
a_class = a_obj.__class__
b_obj = a_class.__new__(cls)
a_class.__init__(b_obj, a_obj) #doesnt work since A's init takes 2 params
Is there a better way than to typecast the given A's object to B?
( i.e a_obj.__classs__ = b_obj )
Edit:
B is a dynamically created class(using type). So B wouldnt know beforehand what are the parameters that A's __init__ would take.