I have 3 classes as such:
class Year1(Student):
def action():
...
class Year2(Student):
def action():
...
class Year3(Student):
def action():
...
How can I instantiate those objects from a list of tuples of their string names and integer values like this :
ls = [('Year1', 10), ('Year2', 15), ('Year3', 25)]
to this:
years = [Year1(10), Year2(15), Year3(25)]
This question didn't help me solve the problem.
('str', int)cls(), you’ll need to pass your argument as well, e.g.cls(age).globals()[cls_name]. That gives you the class object from its name as a string. The rest is up to you and your specific class. Do you understand what this single expression does? If not, play around withprint(globals())…