詳解python單例模式與metaclass
單例模式的實現(xiàn)方式
將類實例綁定到類變量上
class Singleton(object):
_instance = None
def __new__(cls, *args):
if not isinstance(cls._instance, cls):
cls._instance = super(Singleton, cls).__new__(cls, *args)
return cls._instance
但是子類在繼承后可以重寫__new__以失去單例特性
class D(Singleton):
def __new__(cls, *args):
return super(D, cls).__new__(cls, *args)
使用裝飾器實現(xiàn)
def singleton(_cls):
inst = {}
def getinstance(*args, **kwargs):
if _cls not in inst:
inst[_cls] = _cls(*args, **kwargs)
return inst[_cls]
return getinstance
@singleton
class MyClass(object):
pass
問題是這樣裝飾以后返回的不是類而是函數(shù),當然你可以singleton里定義一個類來解決問題,但這樣就顯得很麻煩了
使用__metaclass__,這個方式最推薦
class Singleton(type):
_inst = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._inst:
cls._inst[cls] = super(Singleton, cls).__call__(*args)
return cls._inst[cls]
class MyClass(object):
__metaclass__ = Singleton
metaclass
元類就是用來創(chuàng)建類的東西,可以簡單把元類稱為“類工廠”,類是元類的實例。type就是Python的內(nèi)建元類,type也是自己的元類,任何一個類
>>> type(MyClass) type >>> type(type) type
python在創(chuàng)建類MyClass的過程中,會在類的定義中尋找__metaclass__,如果存在則用其創(chuàng)建類MyClass,否則使用內(nèi)建的type來創(chuàng)建類。對于類有繼承的情況,如果當前類沒有找到,會繼續(xù)在父類中尋找__metaclass__,直到所有父類中都沒有找到才使用type創(chuàng)建類。
如果模塊里有__metaclass__的全局變量的話,其中的類都將以其為元類,親自試了,沒這個作用,無任何影響
查看type的定義,
type(object) -> the object's type
type(name, bases, dict) -> a new type
所以利用type定義一個類的元類,可以用函數(shù)返回一個上面第二種定義的對象,也可以繼承type并重寫其中的方法。
直接使用type生成的對象作為元類,函數(shù)作用是使屬性變?yōu)榇髮?/p>
def update_(name, bases, dct):
attrs = ((name, value) for name, value in dct.items() if not name.startswith('__'))
uppercase_attr = {name.upper(): value for name, value in attrs}
return type(name, bases, uppercase_attr)
class Singleton(object):
__metaclass__ = update_
abc = 2
d = Singleton()
print d.ABC
# 2
上一節(jié)中,單例模式元類實現(xiàn)用的是類繼承方式,而對于第一種__new__的方式,本質(zhì)上調(diào)用的是type.__new__,不過使用super能使繼承更清晰一些并避免一些問題
這里簡單說明一下,__new__是在__init__前調(diào)用的方法,會創(chuàng)建對象并返回,而__init__則是用傳入的參數(shù)將對象初始化??匆幌聇ype中這兩者以及__call__的實現(xiàn)
def __init__(cls, what, bases=None, dict=None): # known special case of type.__init__
"""
type(object) -> the object's type
type(name, bases, dict) -> a new type
# (copied from class doc)
"""
pass
@staticmethod # known case of __new__
def __new__(S, *more): # real signature unknown; restored from __doc__
""" T.__new__(S, ...) -> a new object with type S, a subtype of T """
pass
def __call__(self, *more): # real signature unknown; restored from __doc__
""" x.__call__(...) <==> x(...) """
pass
前面提到類相當于元類的實例化,再聯(lián)系創(chuàng)建單例模式時使用的函數(shù),用的是__call__,其實用三種magic method中任何一種都是可以的,來看一下使用元類時各方法的調(diào)用情況
class Basic(type):
def __new__(cls, name, bases, newattrs):
print "new: %r %r %r %r" % (cls, name, bases, newattrs)
return super(Basic, cls).__new__(cls, name, bases, newattrs)
def __call__(self, *args):
print "call: %r %r" % (self, args)
return super(Basic, self).__call__(*args)
def __init__(cls, name, bases, newattrs):
print "init: %r %r %r %r" % (cls, name, bases, newattrs)
super(Basic, cls).__init__(name, bases, dict)
class Foo:
__metaclass__ = Basic
def __init__(self, *args, **kw):
print "init: %r %r %r" % (self, args, kw)
a = Foo('a')
b = Foo('b')
結(jié)果
new: <class '__main__.Basic'> 'Foo' () {'__module__': '__main__', '__metaclass__': <class '__main__.Basic'>, '__init__': <function init at 0x106fd5320>}
init: <class '__main__.Foo'> 'Foo' () {'__module__': '__main__', '__metaclass__': <class '__main__.Basic'>, '__init__': <function init at 0x106fd5320>}
call: <class '__main__.Foo'> ('a',)
init: <__main__.Foo object at 0x106fee990> ('a',) {}
call: <class '__main__.Foo'> ('b',)
init: <__main__.Foo object at 0x106feea50> ('b',) {}
元類的__init__和__new__只在創(chuàng)建類Foo調(diào)用了一次,而創(chuàng)建Foo的實例時,每次都會調(diào)用元類的__call__方法
以上就是本文的全部內(nèi)容,對python單例模式與metaclass進行了描述,希望對大家的學習有所幫助。
相關(guān)文章
關(guān)于Python turtle庫使用時坐標的確定方法
這篇文章主要介紹了關(guān)于Python turtle庫使用時坐標的確定方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03
利于python腳本編寫可視化nmap和masscan的方法
這篇文章主要介紹了利于python腳本編寫可視化nmap和masscan的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
解決Keras自帶數(shù)據(jù)集與預訓練model下載太慢問題
這篇文章主要介紹了解決Keras自帶數(shù)據(jù)集與預訓練model下載太慢問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
已安裝tensorflow-gpu,但keras無法使用GPU加速的解決
今天小編就為大家分享一篇已安裝tensorflow-gpu,但keras無法使用GPU加速的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Python將json文件寫入ES數(shù)據(jù)庫的方法
這篇文章主要介紹了Python將json文件寫入ES數(shù)據(jù)庫的方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-04-04
Python+Matplotlib繪制高亮顯示餅圖的示例代碼
餅圖 (Pie Chart) 是一種圓形統(tǒng)計圖,被分割成片用于表示數(shù)值間的比例關(guān)系,本文為大家介紹了Matplotlib繪制高亮顯示的餅圖的函數(shù)源碼,需要的可以參考一下2023-06-06
selenium使用chrome瀏覽器測試(附chromedriver與chrome的對應關(guān)系表)
這篇文章主要介紹了selenium使用chrome瀏覽器測試(附chromedriver與chrome的對應關(guān)系表),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11

