如何创建一个只能由类的静态函数调用而不是从其他地方调用的私有(private)构造函数?
最佳答案
_
和 __
前缀不能提供将对象的实例化限制在特定“工厂”的解决方案,但是 Python 是一个强大的工具箱,并且需要行为可以通过不止一种方式实现(正如 Z 的 @Jesse W 所证明的那样)。
这是一个可能的解决方案,可以让类公开可见(允许 isinstance
等),但确保只能通过类方法进行构造:
class OnlyCreatable(object):
__create_key = object()
@classmethod
def create(cls, value):
return OnlyCreatable(cls.__create_key, value)
def __init__(self, create_key, value):
assert(create_key == OnlyCreatable.__create_key), \
"OnlyCreatable objects must be created using OnlyCreatable.create"
self.value = value
用 create
类方法构造一个对象:
>>> OnlyCreatable.create("I'm a test")
<__main__.OnlyCreatable object at 0x1023a6f60>
当试图在不使用 create
的情况下构造对象时,由于断言,类方法创建失败:
>>> OnlyCreatable(0, "I'm a test")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 11, in __init__
AssertionError: OnlyCreatable objects can only be created using OnlyCreatable.create
如果试图通过模仿 create
类方法来创建对象
由于 OnlyCreatable.__createKey
的编译器错误,创建失败。
>>> OnlyCreatable(OnlyCreatable.__createKey, "I'm a test")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'OnlyCreatable' has no attribute '__createKey'
在类方法之外构造 OnlyCreatable
的唯一方法是知道 OnlyCreatable.__create_key
的值。由于此类属性的值是在运行时生成的,并且它的名称以 __ 为前缀,将其标记为不可访问,因此实际上“不可能”获取该值和/或构造对象。
关于python - Python中的私有(private)构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8212053/