不确定这是否是您想要的,但是似乎更Python化的方式来实例化字符串中列出的一堆类:
class idClasses: class ID12345:pass class ID01234:pass# could also be: import idClassesclass ProcessDirector: def __init__(self): self.allClasses = [] def construct(self, builderName): targetClass = getattr(idClasses, builderName) instance = targetClass() self.allClasses.append(instance)IDS = ["ID12345", "ID01234"]director = ProcessDirector()for id in IDS: director.construct(id)print director.allClasses# [<__main__.ID12345 instance at 0x7d850>, <__main__.ID01234 instance at 0x7d918>]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)