I would like to create a subclass instance from a superclass instance in Python. Suppose I have something like this:
class A():
def __init__(self, type):
...
self.type = type # this will be something that corresponds to either B or C
class B(A):
def do_something():
# this method is subclass specific
class C(A):
def do_something():
# this method is again subclass specific
I have a function that receives an instance of A, and I need to create an instance of of either B or C (but there are more subclasses in reality) based on what A's attribute type is. I'm not sure how to go about this. Is there a way out of this or does the solution need to be redesigned?
I should mentioned that I'm working with python 2.7 on this issue.
Thank you
Aucun commentaire:
Enregistrer un commentaire