當python,單例模式,多例模式,一次初始化遇到一起

來源:互聯網
上載者:User

標籤:nis   finish   instance   pre   self   rgs   code   執行個體   nbsp   

1.在python中,單例模式是很容易實現的,隨便翻翻網上的相關教程,就能夠找到很多答案。

比如這樣:

class hello(object):    def __new__(cls, *args, **kwargs):        if not ‘_instance_one‘ in vars(cls):            cls._instance_one=object.__new__(cls)            return cls._instance_one        return cls._instance_one    def __init__(self):        print selfa=hello()b=hello()#************************** result *******************************<__main__.hello object at 0x7f8afeaf1150><__main__.hello object at 0x7f8afeaf1150>Process finished with exit code 0

可以看到,兩個執行個體的記憶體位址相同,即表示二者是同一個執行個體 。

注意:如果我們重寫__new__函數的話,需要繼承object類。

 

2.需要注意到的是 上例中的self和cls._instance_one實際是同一個東西,我們可以簡單做一個測試一下:

class hello(object):    def __new__(cls, *args, **kwargs):        if not ‘_instance_one‘ in vars(cls):            cls._instance_one=object.__new__(cls)            print cls._instance_one            return cls._instance_one        return cls._instance_one    def __init__(self):        print selfa=hello()#************************************** result **********************************<__main__.hello object at 0x7fb31f65e150><__main__.hello object at 0x7fb31f65e150>Process finished with exit code 0

 

3.如果我們需要讓單例模式只初始化一次的話,我們只需要加一個標誌即可:

class hello(object):    def __new__(cls, *args, **kwargs):        if not ‘_instance_one‘ in vars(cls):            cls._instance_one=object.__new__(cls)            cls._instance_one._flag=1            return cls._instance_one        return cls._instance_one    def __init__(self):        if self._flag:            print self            self._flag=0        print "end"a=hello()b=hello()#************************************result*********************************<__main__.hello object at 0x7f14de3bd150>endend

 

4.注意到上例中的_flag寫在類內,類外都可以,我們同樣可以做一個實驗:

class hello(object):    _flag=1    def __new__(cls, *args, **kwargs):        if not ‘_instance_one‘ in vars(cls):            cls._instance_one=object.__new__(cls)            return cls._instance_one        if not ‘_instance_two‘ in vars(cls):            cls._instance_two=object.__new__(cls)            return cls._instance_two    def __init__(self):        print self._flag        self._flag=0        print self._flaga=hello()b=hello()#*************************************result ***************************************1010Process finished with exit code 0

 可以看到二者是等價的。

 

當python,單例模式,多例模式,一次初始化遇到一起

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.