一、单例模式
如果你想保证某个类从始至终最多只能有一个实例,那么单例模式可能会是你首先想到的,使用__new__静态方法可以很简单的解决:
class Singleton(object): def __new__(cls,*args,**kwargs): if '_inst' not in vars(cls): cls._inst = super(Singleton,cls).__new__(cls) return cls._inst
Python 2.X 里你可能看到的实现代码:
class Singleton(object): def __new__(cls,*args,**kwargs): if '_inst' not in vars(cls): cls._inst = super(Singleton,cls).__new__(cls,*args,*kwargs) return cls._inst
它们这间的细微不同之处在于 *args,**kwargs:
cls._inst = super(Singleton,cls).__new__(cls,*args,**kwargs)
而这在Python 3.x里将会引发异常:TypeError: object() takes no parameters
如果我们查看 CPython 源码,我们将会看到:
(1)在__new__ 被overridden或者__init__没有被overridden 的情况下,如果调用 object.__new__的时候传递了除cls之外的参数将会报错;
(2)在__new__ 没有被overridden或者__init__被overridden 的情况下,如果调用 object.__init__ 的时候传递了除cls之外的参数将会报错;
(3)*args,和*kwds 在object.__new__除了用来判断报错,并没有什么其它用处
static int object_init(PyObject *self, PyObject *args, PyObject *kwds) { int err = 0; PyTypeObject *type = Py_TYPE(self); if (excess_args(args, kwds) && (type->tp_new == object_new || type->tp_init != object_init)) { PyErr_SetString(PyExc_TypeError, "object.__init__() takes no parameters"); err = -1; } return err; } static PyObject * object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { if (excess_args(args, kwds) && (type->tp_init == object_init || type->tp_new != object_new)) { PyErr_SetString(PyExc_TypeError, "object() takes no parameters"); return NULL; } ... }
二、Borg惯用法
如果你想保证某个类从始至终只创建了一个实例:你并不关心生成实例的id,只关心其状态和行为方式,而且你还想确保它具有子类化的能力,那么可以使用Borg惯用法:
class Borg(object): _state = {} def __new__(cls,*args,**kwargs): obj = super(SingletonBorg,cls).__new__(cls) obj.__dict__ = cls._state return obj
和单例模式不同的是,Borg惯用法允许多个实例被创建,但所有的实例都共享状态和行为方式。通过这种“数据重载”,你的类不会从Borg继承_shared_state属性,而是自己的数据。为了允许这种“数据重载”,Borg的__new__应当使用cls._shared_state,而不是Borg._shared_state。
如果你想Borg的所有类实例都能够共享状态:
class SingletonSpam_02(Borg):pass
如果你想你的类实例能够相互共享状态,但却不和Borg的其它子类共享,需要在类作用域中加入这样的声明:
class SingletonSpam_03(Borg): _state = {}
测试:
if __name__ == '__main__': class SingletonSpam_02(Borg):pass class SingletonSpam_03(Borg): _state = {} one = SingletonSpam_02() two = SingletonSpam_02() one.name = 'lilei' two.name = 'hanmeimei' print(one.name,two.name) three = SingletonSpam_03() four = SingletonSpam_03() three.name = 'liuxing' four.name = 'baiyun' print(three.name,four.name)
执行后,发现在子类中加入_state = {} 就可以实现:自己的类实例能够相互共享状态,但却不和Borg的其它子类共享:
('hanmeimei', 'hanmeimei') ('baiyun', 'baiyun')