2
class Factory:
def get_singleton(self, class_name):
if class_name not in Factory.__dict__:
new_instance = self.get_new_instance(class_name)
new_attribute = self.get_attribute_name_from_class_name(class_name)
Factory.__setattr__(Factory, new_attribute, new_instance)
return Factory.__getattribute__(new_attribute)
我想提出一个对象工厂类,并在上面我get_singleton功能,我有这样一行:跨实例动态命名的单属性在Python
是 SETATTR想要一个实例Factory.__setattr__(Factory, new_attribute, new_instance)
文档说对于第一个参数,但我希望能够跨实例设置动态命名的属性。这样下一次我调用get_singleton函数时,它将返回我在以前的调用中创建的同一个类实例。我希望能够跨实例动态命名单例属性。
这是我如何调用该函数来自外部:
manager = Factory().get_singleton('Manager')
有没有办法在Python做到这一点?
谢谢