In my project, I'm creating a base_class object first, but later on the program it should become a subclass. In the example, first I created an animal
, later I've decided that it should become a dog
. In this case, I don't wanna delete my old animal
object and create a new dog object which may have super()
function in it, because I don't want to pass all animal
creation arguments all over again.
So is there any better method other than shown in the example to code something like this.
class animal:
def __init__(self, H):
self.hight = H
print("animal created @ height : ", self.hight)
x = animal(10)
#animal created @ height : 10
class dog: #with cheap trick
def __init__(self, animal_object, color):
self.animal_object = animal_object
self.color = color
print("animal became a dog")
print("dog's @ color : ", self.color)
print("dog's @ heigth : ", self.animal_object.hight)
y = dog(x, "black") #animal becomes a dog
# animal became a dog
# dog's @ color : black
# dog's @ heigth : 10
class tranform_to_dog(animal): #super() method
def __init__(self, H, color):
super().__init__(H)
self.color = color
print("Dog created as a subclass of animal, heigth: {}, color:{}".format(self.hight,self.color))
z = tranform_to_dog(8, "white")
#Dog created as a subclass of animal, heigth: 8, color:white
Above, I want to keep going with X
object which is already created as an animal
and call tranform_to_dog
method with X so I can get z
like subclassed object