I have a use case where I want to get another class based upon the type of object.
For one solution, I have created a dict where I am maintaining this relation and using type to match among classes.
For example, consider the below code
class classA(object):
def __init__(self):
print "A called"
class classB(object):
def __init__(self):
print "B called"
MAP = {
A: classA,
B: classB
}
class C(object):
pass
class A(C):
pass
class B(C):
pass
obj can be object of A or B.
new_cls = MAP.get(type(obj), 'None')
new_obj = new_cls()
Is there a better way to achieve this?