Creating classes inside a loop Python
问题 I'm working on a Python project and I want to do something like the next example, but is incorrect. I need some help, please! names = ['name1', 'name2'] for name in names: class name: [statements] Thank you! 回答1: The class statement requires a hard-coded class name. You can use the type function, however, to create such dynamic classes. names = ['name1', 'name2'] class_dict = {} for name in names: # statements to prepare d class_dict[name] = type(name, (object,), d) Here, d is a dictionary