I have the following code:
def f(cls, value):
# cls is a class
# value is a string value
if cls == str:
pass # value is already the right type
elif cls == int:
value = int(value)
elif cls == C1:
value = C1(value)
elif cls == C2:
value = C2(value)
elif cls == C3
# in this case, we convert the string into a list of objects of class C3
value = C3.parse(value)
else
raise UnknownClass(repr(cls))
return value
Obviously, I'm trying to replace it with something like:
def f(cls, value)
return cls(value)
Unfortunately, in some cases (if cls == C3), the parsing of the input results in a list of objects of that class, rather than just one object. What's a neat way to handle this? I have access to all the classes.