这可能是一个简单的问题,但我无法在任何地方找到答案。我想接受一个参数或输入并使用该名称来创建一个类的实例。那可能吗?
示例:
def make_instance(argument):
# argument = Something() | example if argument = "one", then
# make instance would create--> one = Something()
# and I could do one.do_anything
class Something:
def do_anything():
print("anything")
这可能吗?解释参数的内容并使用它来创建类或变量的实例或任何事情。只要参数的内容(无论是int还是str)成为类的实例名称或变量名称?
答案 0 :(得分:2)
您可能想要做类似
的事情instances = {}
key3 = 12345
instances["one"] = Something()
instances[2] = Something() # another instance
instances[key3] = Something() # and another one