Jam*_*mie 427 python singleton design-patterns
似乎有很多方法可以在Python中定义单例.Stack Overflow是否有共识?
Sta*_*ale 346
我并不真正看到需要,因为具有函数(而不是类)的模块可以很好地用作单例.它的所有变量都将绑定到模块,无论如何都无法重复实例化.
如果您确实希望使用类,则无法在Python中创建私有类或私有构造函数,因此除了通过使用API的约定之外,您无法防止多个实例化.我仍然只是将方法放在一个模块中,并将模块视为单例.
Pau*_*nta 289
这是我自己实现的单身人士.你所要做的就是装饰课程; 要获得单例,您必须使用该Instance方法.这是一个例子:
@Singleton
class Foo:
def __init__(self):
print 'Foo created'
f = Foo() # Error, this isn't how you get the instance of a singleton
f = Foo.instance() # Good. Being explicit is in line with the Python Zen
g = Foo.instance() # Returns already created instance
print f is g # True
Run Code Online (Sandbox Code Playgroud)
这是代码:
class Singleton:
"""
A non-thread-safe helper class to ease implementing singletons.
This should be used as a decorator -- not a metaclass -- to the
class that should be a singleton.
The decorated class can define one `__init__` function that
takes only the `self` argument. Also, the decorated class cannot be
inherited from. Other than that, there are no restrictions that apply
to the decorated class.
To get the singleton instance, use the `instance` method. Trying
to use `__call__` will result in a `TypeError` being raised.
"""
def __init__(self, decorated):
self._decorated = decorated
def instance(self):
"""
Returns the singleton instance. Upon its first call, it creates a
new instance of the decorated class and calls its `__init__` method.
On all subsequent calls, the already created instance is returned.
"""
try:
return self._instance
except AttributeError:
self._instance = self._decorated()
return self._instance
def __call__(self):
raise TypeError('Singletons must be accessed through `instance()`.')
def __instancecheck__(self, inst):
return isinstance(inst, self._decorated)
Run Code Online (Sandbox Code Playgroud)
joj*_*ojo 183
您可以__new__像这样覆盖方法:
class Singleton(object):
_instance = None
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(Singleton, cls).__new__(
cls, *args, **kwargs)
return cls._instance
if __name__ == '__main__':
s1 = Singleton()
s2 = Singleton()
if (id(s1) == id(s2)):
print "Same"
else:
print "Different"
Run Code Online (Sandbox Code Playgroud)
Pet*_*ann 110
在Python中实现单例的略微不同的方法是Alex Martelli(Google员工和Python天才)的borg模式.
class Borg:
__shared_state = {}
def __init__(self):
self.__dict__ = self.__shared_state
Run Code Online (Sandbox Code Playgroud)
因此,它们不是强制所有实例具有相同的身份,而是共享状态.
小智 81
模块方法运作良好.如果我绝对需要单身人士,我更喜欢Metaclass方法.
class Singleton(type):
def __init__(cls, name, bases, dict):
super(Singleton, cls).__init__(name, bases, dict)
cls.instance = None
def __call__(cls,*args,**kw):
if cls.instance is None:
cls.instance = super(Singleton, cls).__call__(*args, **kw)
return cls.instance
class MyClass(object):
__metaclass__ = Singleton
Run Code Online (Sandbox Code Playgroud)
小智 43
从PEP318看到这个实现,用装饰器实现单例模式:
def singleton(cls):
instances = {}
def getinstance():
if cls not in instances:
instances[cls] = cls()
return instances[cls]
return getinstance
@singleton
class MyClass:
...
Run Code Online (Sandbox Code Playgroud)
Lam*_*iry 26
正如公认的答案所说,最惯用的方法是使用模块.
考虑到这一点,这是一个概念证明:
def singleton(cls):
obj = cls()
# Always return the same object
cls.__new__ = staticmethod(lambda cls: obj)
# Disable __init__
try:
del cls.__init__
except AttributeError:
pass
return cls
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,请参阅Python数据模型__new__.
例:
@singleton
class Duck(object):
pass
if Duck() is Duck():
print "It works!"
else:
print "It doesn't work!"
Run Code Online (Sandbox Code Playgroud)
笔记:
您必须为此使用新式类(派生自object).
单例在定义时初始化,而不是在第一次使用时初始化.
这只是一个玩具的例子.我从来没有在生产代码中实际使用它,也不打算这样做.
u0b*_*6ae 20
我对此非常不确定,但我的项目使用'convention singletons'(非强制单例),也就是说,如果我有一个叫做的类DataController,我在同一个模块中定义它:
_data_controller = None
def GetDataController():
global _data_controller
if _data_controller is None:
_data_controller = DataController()
return _data_controller
Run Code Online (Sandbox Code Playgroud)
它不优雅,因为它是完整的六行.但我所有的单身人士都使用这种模式,至少非常明确(这是pythonic).
Bri*_*man 20
在Python文档确实涉及到这一点:
class Singleton(object):
def __new__(cls, *args, **kwds):
it = cls.__dict__.get("__it__")
if it is not None:
return it
cls.__it__ = it = object.__new__(cls)
it.init(*args, **kwds)
return it
def init(self, *args, **kwds):
pass
Run Code Online (Sandbox Code Playgroud)
我可能会重写它看起来更像这样:
class Singleton(object):
"""Use to create a singleton"""
def __new__(cls, *args, **kwds):
"""
>>> s = Singleton()
>>> p = Singleton()
>>> id(s) == id(p)
True
"""
self = "__self__"
if not hasattr(cls, self):
instance = object.__new__(cls)
instance.init(*args, **kwds)
setattr(cls, self, instance)
return getattr(cls, self)
def init(self, *args, **kwds):
pass
Run Code Online (Sandbox Code Playgroud)
扩展它应该相对干净:
class Bus(Singleton):
def init(self, label=None, *args, **kwds):
self.label = label
self.channels = [Channel("system"), Channel("app")]
...
Run Code Online (Sandbox Code Playgroud)
Dav*_*cke 15
有一次我在Python中编写了一个单例,我使用了一个类,其中所有成员函数都有classmethod装饰器.
class foo:
x = 1
@classmethod
def increment(cls, y = 1):
cls.x += y
Run Code Online (Sandbox Code Playgroud)
Mat*_*ock 10
如果要在未来装饰(注释)类,创建单例装饰器(也称为注释)是一种优雅的方式.然后你就把@singleton放在你的类定义之前.
def singleton(cls):
instances = {}
def getinstance():
if cls not in instances:
instances[cls] = cls()
return instances[cls]
return getinstance
@singleton
class MyClass:
...
Run Code Online (Sandbox Code Playgroud)
我认为强迫一个类或一个实例成为一个单例是过度的.就个人而言,我喜欢定义一个普通的可实例化类,一个半私有引用和一个简单的工厂函数.
class NothingSpecial:
pass
_the_one_and_only = None
def TheOneAndOnly():
global _the_one_and_only
if not _the_one_and_only:
_the_one_and_only = NothingSpecial()
return _the_one_and_only
Run Code Online (Sandbox Code Playgroud)
或者,如果在首次导入模块时实例化没有问题:
class NothingSpecial:
pass
THE_ONE_AND_ONLY = NothingSpecial()
Run Code Online (Sandbox Code Playgroud)
这样,您可以针对没有副作用的新实例编写测试,并且不需要在模块中添加全局语句,如果需要,您可以在将来派生变体.
| 归档时间: |
|
| 查看次数: |
265487 次 |
| 最近记录: |