How to call class constructor having its name in text variable? [Python]
问题 Let's assume we have some classes defined and available in global namespace. In example: class Point: def __init__(self, x, y): self.x = x self.y = y class Vector: def __init__(self, alpha, r): self.x = r * cos(alpha) self.y = r * sin(alpha) # and many others... How to do this: class_name = 'Point' x = 14.361 y = -8.100 code_str = 'class_object = ' + class_name + '(' + str(x) + ', ' + str(y) + ')' exec code_str # That evaluates to: "class_object = Point(14.361, -8.100)" print class_object.x,