1. 程式人生 > >Python 單例

Python 單例

cal obj 如果 def ins __call__ class type n)

方法一

  實現__new__方法,然後將類的一個實例綁定到類變量_instance上;如果cls._instance為None,則說明該類還沒有被實例化過,new一個該類的實例,並返回;如果cls._instance不為None,直接返回_instance,代碼如下

class Singleton(object):
  
  def __new__(cls, *args, **kwargs):
    if not hasattr(cls, _instance):
      orig = super(Singleton, cls)
      cls._instance 
= orig.__new__(cls, *args, **kwargs) return cls._instance class MyClass(Singleton): a = 1 one = MyClass() two = MyClass() #one和two完全相同,可以用id(), ==, is檢測 print id(one) # 29097904 print id(two) # 29097904 print one == two # True print one is two # True

方法二

  本質上是方法一的升級版,使用__metaclass__(元類)的高級python用法,具體代碼如下:

class Singleton2(type):
  
  def __init__(cls, name, bases, dict):
    super(Singleton2, cls).__init__(name, bases, dict)
    cls._instance = None
  
  def __call__(cls, *args, **kwargs):
    if cls._instance is None:
      cls._instance = super(Singleton2, cls).__call__(*args, **kwargs)
    
return cls._instance class MyClass2(object): __metaclass__ = Singleton2 a = 1 one = MyClass2() two = MyClass2() print id(one) # 31495472 print id(two) # 31495472 print one == two # True print one is two # True

方法三

  使用Python的裝飾器(decorator)實現單例模式,這是一種更Pythonic的方法;單利類本身的代碼不是單例的,通裝飾器使其單例化,代碼如下:

  

def singleton(cls, *args, **kwargs):
  instances = {}
  def _singleton():
    if cls not in instances:
      instances[cls] = cls(*args, **kwargs)
    return instances[cls]
  return _singleton
  
@singleton
class MyClass3(object):
  a = 1
  
one = MyClass3()
two = MyClass3()
  
print id(one)  # 29660784
print id(two)  # 29660784
print one == two  # True
print one is two  # True

  

Python 單例