Initialize subclass within class in python
问题 I am initializing a class along with two subclasses in Python using a dictionary. Would it be possible to check a key in the dictionary within the init and depending the result initialize either one of the two subclasses? For instance: Class Pet(): def __init__(self,dic): self.name=dic['name'] self.age=dic['age'] if dic['type']=='dog': #Initialize a dog which inherits all pet methods with name and age passed onto it elif dic['type']=='cat': #Initialize a dog which inherits all pet methods