Python设计模式之单例模式实例

 更新时间:2014年04月26日 11:30:26   作者:  
这篇文章主要介绍了设计模式中的单例模式Python实例,需要的朋友可以参考下

注:使用的是Python 2.7。

一个简单实现

复制代码 代码如下:

class Foo(object):
    __instance = None
    def __init__(self):
        pass
    @classmethod
    def getinstance(cls):
        if(cls.__instance == None):
            cls.__instance = Foo()
        return cls.__instance

if __name__ == '__main__':
    foo1 = Foo.getinstance()
    foo2 = Foo.getinstance()
    print id(foo1)
    print id(foo2)
    print id(Foo())


输出的前两个结果是相同的(id(foo1)与id(foo2)的值相同),第三个结果和前两个不同。这里类方法getinstance()用于获取单例,但是类本身也可以实例化,这样的方式其实并不符合单例模式的要求。但是这样做也有好处,代码简单,大家约定好这样子调用就行了。但是最好在类的命名上也体现了出来这是一个单例类,例如Foo_singleton。

换一个思路

先说一下init和new的区别:

复制代码 代码如下:

class Foo(object):
    __instance = None
    def __init__(self):
        print 'init'
if __name__ == '__main__':
    foo = Foo()

运行结果是:
复制代码 代码如下:

init

而下面的示例:
复制代码 代码如下:

class Foo(object):
    __instance = None
    def __init__(self):
        print 'init'
    def __new__(cls, *args, **kwargs):
        print 'new'

if __name__ == '__main__':
    foo = Foo()


运行结果是:
复制代码 代码如下:
new

new是一个类方法,会创建对象时调用。而init方法是在创建完对象后调用,对当前对象的实例做一些一些初始化,无返回值。如果重写了new而在new里面没有调用init或者没有返回实例,那么init将不起作用。以下内容引用自http://docs.python.org/2/reference/datamodel.html#object.new

复制代码 代码如下:

If __new__() returns an instance of cls, then the new instance's __init__() method will be invoked like __init__(self[, ...]), where self is the new instance and the remaining arguments are the same as were passed to __new__().

If __new__() does not return an instance of cls, then the new instance's __init__() method will not be invoked.


这样做:
复制代码 代码如下:

class Foo(object):
    __instance = None
    def __init__(self):
        print 'init'

    def __new__(cls, *args, **kwargs):
        print 'new'
        if cls.__instance == None:
            cls.__instance = cls.__new__(cls, *args, **kwargs)
        return cls.__instance

if __name__ == '__main__':
    foo = Foo()

    错误如下:

复制代码 代码如下:

RuntimeError: maximum recursion depth exceeded in cmp

而这样也有一样的错误:

复制代码 代码如下:

class Foo(object):
    __instance = None
    def __init__(self):
        if self.__class__.__instance == None:
            self.__class__.__instance = Foo()
        print 'init'

if __name__ == '__main__':
    foo = Foo()


该怎么做呢?

下面参考了http://stackoverflow.com/questions/31875/is-there-a-simple-elegant-way-to-define-singletons-in-python/31887#31887:

复制代码 代码如下:

class Foo(object):
    __instance = None
    def __new__(cls, *args, **kwargs):
        print 'hhhhhhhhh'
        if not cls.__instance:
            cls.__instance = super(Foo, cls).__new__(cls, *args, **kwargs)
        return cls.__instance

    def hi(self):
        print 'hi, world'
        print 'hi, letian'

if __name__ == '__main__':
    foo1 = Foo()
    foo2 = Foo()
    print id(foo1)
    print id(foo2)
    print isinstance(foo1, object)
    print isinstance(foo1, Foo)
    foo1.hi()


运行结果:
复制代码 代码如下:

hhhhhhhhh
hhhhhhhhh
39578896
39578896
True
True
hi, world
hi, letian

那么,到底发生了什么,我们先回顾一下super:

复制代码 代码如下:

>>> print super.__doc__
super(type) -> unbound super object
super(type, obj) -> bound super object; requires isinstance(obj, type)
super(type, type2) -> bound super object; requires issubclass(type2, type)
Typical use to call a cooperative superclass method:
class C(B):
    def meth(self, arg):
        super(C, self).meth(arg)

可以肯定上面的单例模式代码中的这一行代码:
复制代码 代码如下:

cls.__instance = super(Foo, cls).__new__(cls, *args, **kwargs)

super(Foo, cls)是object,super(Foo, cls).new方法使用的是object的new方法。我们看一下object.new方法的作用:
复制代码 代码如下:

>>> print object.__new__.__doc__
T.__new__(S, ...) -> a new object with type S, a subtype of T

如果是一个继承链

复制代码 代码如下:

class Fo(object):
    def __new__(cls, *args, **kwargs):
        print 'hi, i am Fo'
        return  super(Fo, cls).__new__(cls, *args, **kwargs)

class Foo(Fo):
    __instance = None
    def __new__(cls, *args, **kwargs):
        if not cls.__instance:
            print Foo is cls
            print issubclass(cls, Fo)
            print issubclass(cls, object)
            cls.__instance = super(Foo, cls).__new__(cls, *args, **kwargs)
        return cls.__instance

    def hi(self):
        print 'hi, world'

if __name__ == '__main__':
    foo1 = Foo()
    foo1.hi()
    print isinstance(foo1, Foo)
    print isinstance(foo1, Fo)
    print isinstance(foo1, object)


运行结果如下:
复制代码 代码如下:

True
True
True
hi, i am Fo
hi, world
True
True
True

如果如下定义Fo,也正常运行:
复制代码 代码如下:

class Fo(object):
    pass

但是,若这样定义:
复制代码 代码如下:

class Fo(object):
    def __new__(cls, *args, **kwargs):
        print 'hi, i am Fo'

运行时报错如下:
复制代码 代码如下:

AttributeError: 'NoneType' object has no attribute 'hi'

相关文章

  • Pycharm虚拟环境创建并使用命令行指定库的版本进行安装

    Pycharm虚拟环境创建并使用命令行指定库的版本进行安装

    Pycharm创建的项目,使用了虚拟环境,对库的版本进行管理,有些项目的对第三方库的版本要求不同,可使用虚拟环境进行管理,直接想通过pip命令安装可以参考下本文的操作步骤
    2022-07-07
  • python实现简单俄罗斯方块游戏

    python实现简单俄罗斯方块游戏

    这篇文章主要为大家详细介绍了python实现简单俄罗斯方块游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • Python实现多维数据分析的示例详解

    Python实现多维数据分析的示例详解

    多维数据分析是对数据的信息分析,它考虑了许多关系,这篇文章主要为大家详细介绍了一些使用Python分析多维/多变量数据的基本技术,希望对大家有所帮助
    2023-11-11
  • Python 多个图同时在不同窗口显示的实现方法

    Python 多个图同时在不同窗口显示的实现方法

    今天小编就为大家分享一篇Python 多个图同时在不同窗口显示的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-07-07
  • python实现录制全屏和选择区域录屏功能

    python实现录制全屏和选择区域录屏功能

    这篇文章主要介绍了python实现录制全屏和选择区域录屏功能,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-02-02
  • 浅谈Selenium+Webdriver 常用的元素定位方式

    浅谈Selenium+Webdriver 常用的元素定位方式

    这篇文章主要介绍了浅谈Selenium+Webdriver 常用的元素定位方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • python解析Dwarf2格式ELF文件示例

    python解析Dwarf2格式ELF文件示例

    这篇文章主要为大家介绍了python解析Dwarf2格式ELF文件示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • Python轻量级ORM框架Peewee访问sqlite数据库的方法详解

    Python轻量级ORM框架Peewee访问sqlite数据库的方法详解

    这篇文章主要介绍了Python轻量级ORM框架Peewee访问sqlite数据库的方法,结合实例形式较为详细的分析了ORM框架的概念、功能及peewee的安装、使用及操作sqlite数据库的方法,需要的朋友可以参考下
    2017-07-07
  • python中for循环输出列表索引与对应的值方法

    python中for循环输出列表索引与对应的值方法

    今天小编就为大家分享一篇python中for循环输出列表索引与对应的值方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-11-11
  • pyqt5 获取显示器的分辨率的方法

    pyqt5 获取显示器的分辨率的方法

    今天小编就为大家分享一篇pyqt5 获取显示器的分辨率的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-06-06

最新评论