欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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的區(qū)別:

復制代碼 代碼如下:

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是一個類方法,會創(chuàng)建對象時調用。而init方法是在創(chuàng)建完對象后調用,對當前對象的實例做一些一些初始化,無返回值。如果重寫了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

那么,到底發(fā)生了什么,我們先回顧一下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'

相關文章

  • vscode中配置jupyter的詳細步驟(徹底解決Failed?to?start?the?Kernel問題)

    vscode中配置jupyter的詳細步驟(徹底解決Failed?to?start?the?Kernel問題)

    自從vscode出了支持jupyter?notebook的功能之后,我就再也沒有傻傻的用瀏覽器開過jupyter(問就是vscode好看),下面這篇文章主要給大家介紹了關于vscode中配置jupyter(徹底解決Failed?to?start?the?Kernel問題)的相關資料,需要的朋友可以參考下
    2022-12-12
  • OpenMV與JSON編碼問題解析

    OpenMV與JSON編碼問題解析

    這篇文章主要介紹了OpenMV與JSON編碼,JSON是一種簡潔高效的交換數據的格式,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2022-06-06
  • Django自動注冊tasks及使用方式

    Django自動注冊tasks及使用方式

    這篇文章主要為大家介紹了Django自動注冊tasks及使用方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • 使用Python將Mysql的查詢數據導出到文件的方法

    使用Python將Mysql的查詢數據導出到文件的方法

    今天小編就為大家分享一篇關于使用Python將Mysql的查詢數據導出到文件的方法,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • python3 sqlite3限制條件查詢的操作

    python3 sqlite3限制條件查詢的操作

    這篇文章主要介紹了python3 sqlite3限制條件查詢的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • Python深度學習之圖像標簽標注軟件labelme詳解

    Python深度學習之圖像標簽標注軟件labelme詳解

    這篇文章主要介紹了Python深度學習之圖像標簽標注軟件labelme詳解,文中有非常詳細的代碼示例,對正在學習python的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • 淺談django orm 優(yōu)化

    淺談django orm 優(yōu)化

    這篇文章主要介紹了淺談django orm 優(yōu)化,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • Matplotlib繪圖基礎之配置參數詳解

    Matplotlib繪圖基礎之配置參數詳解

    Matplotlib?提供了大量配置參數,這些參數可以但不限于讓我們從整體上調整通過?Matplotlib?繪制的圖形樣式,下面我們就來看看如何巧妙的運用這些參數吧
    2023-08-08
  • Python合并ts文件至mp4格式及解密教程詳解

    Python合并ts文件至mp4格式及解密教程詳解

    m3u8準確來說是一種索引文件,使用m3u8文件實際上是通過它來解析對應的放在服務器上的視頻網絡地址,從而實現在線播放。本文給大家介紹Python合并ts文件至mp4格式及解密教程,需要的朋友參考下吧
    2021-07-07
  • win10環(huán)境下配置vscode python開發(fā)環(huán)境的教程詳解

    win10環(huán)境下配置vscode python開發(fā)環(huán)境的教程詳解

    這篇文章主要介紹了win10環(huán)境下配置python開發(fā)環(huán)境(vscode)的教程,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-10-10

最新評論