假设我有这个简单的课程:
class person:
def __init__(self, name, id):
self.name = name
self.id = id
以及以下实例:
tom = person('tom', 12)
dick = person('dick', 14)
harry = person('harry', 16)
但我希望我的模块的用户能够创建这些人的多个实例,而不必调用 person
构造函数,因为 name
和 id
应仅在一个位置声明。
选项:
使用
copy
或deepcopy
。这将提供我需要的功能,但每次我想使用tom
时,我都必须记住创建他的副本。这太笨重了。创建
tom
类
class Tom(Person):
def __init__(self):
super().__init__('Tom', 12)
这更干净一些,因为每次我想要一个新的 tom
时,我都可以执行 tom()
,但这需要编写大量代码,而且不是很干燥。
python 中是否有其他语法糖可以使这种事情变得更容易?
正确答案
我认为这是工厂/注册表类型模式的一个很好的用例。这个想法是为类和单个注册表字典实现一个替代的方便构造函数,该字典存储与给定标签相对应的配置。看一下下面的例子:
person_registry = {
"tom": {"name": "tom", "id": 12},
"jerry": {"name": "jerry", "id": 13},
"dick": {"name": "dick", "id": 14},
}
class person:
def __init__(self, name, id):
self.name = name
self.id = id
@classmethod
def from_tag(cls, tag):
"""create a person from a tag"""
if tag not in person_registry:
raise valueerror(f"not a valid tag {tag}, choose from {list(person_registry)}")
return cls(**person_registry[tag])
print(person.from_tag("tom"))
现在您可以导入 person
类并从较短的标签创建实例,而实际数据存储在一个位置,即 person_registry
字典中。如果你想减少冗长,你也可以
使用位置参数,但关键字参数通常更可取,因为更明确(python 的 zen)。
这种模式的优点在于它实际上是可扩展的。因此用户可以扩展 person_registry
字典,例如:
from persons import Person, PERSON_REGISTRY
PERSON_REGISTRY["liz"] = {"name": "elizabeth", "id": 23}
liz = Person.from_tag("liz")
我希望这会有所帮助。