W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
你想通過改變實(shí)例創(chuàng)建方式來實(shí)現(xiàn)單例、緩存或其他類似的特性。
Python程序員都知道,如果你定義了一個(gè)類,就能像函數(shù)一樣的調(diào)用它來創(chuàng)建實(shí)例,例如:
class Spam:
def __init__(self, name):
self.name = name
a = Spam('Guido')
b = Spam('Diana')
如果你想自定義這個(gè)步驟,你可以定義一個(gè)元類并自己實(shí)現(xiàn) __call__()
方法。
為了演示,假設(shè)你不想任何人創(chuàng)建這個(gè)類的實(shí)例:
class NoInstances(type):
def __call__(self, *args, **kwargs):
raise TypeError("Can't instantiate directly")
# Example
class Spam(metaclass=NoInstances):
@staticmethod
def grok(x):
print('Spam.grok')
這樣的話,用戶只能調(diào)用這個(gè)類的靜態(tài)方法,而不能使用通常的方法來創(chuàng)建它的實(shí)例。例如:
>>> Spam.grok(42)
Spam.grok
>>> s = Spam()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "example1.py", line 7, in __call__
raise TypeError("Can't instantiate directly")
TypeError: Can't instantiate directly
>>>
現(xiàn)在,假如你想實(shí)現(xiàn)單例模式(只能創(chuàng)建唯一實(shí)例的類),實(shí)現(xiàn)起來也很簡單:
class Singleton(type):
def __init__(self, *args, **kwargs):
self.__instance = None
super().__init__(*args, **kwargs)
def __call__(self, *args, **kwargs):
if self.__instance is None:
self.__instance = super().__call__(*args, **kwargs)
return self.__instance
else:
return self.__instance
# Example
class Spam(metaclass=Singleton):
def __init__(self):
print('Creating Spam')
那么Spam類就只能創(chuàng)建唯一的實(shí)例了,演示如下:
>>> a = Spam()
Creating Spam
>>> b = Spam()
>>> a is b
True
>>> c = Spam()
>>> a is c
True
>>>
最后,假設(shè)你想創(chuàng)建8.25小節(jié)中那樣的緩存實(shí)例。下面我們可以通過元類來實(shí)現(xiàn):
import weakref
class Cached(type):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.__cache = weakref.WeakValueDictionary()
def __call__(self, *args):
if args in self.__cache:
return self.__cache[args]
else:
obj = super().__call__(*args)
self.__cache[args] = obj
return obj
# Example
class Spam(metaclass=Cached):
def __init__(self, name):
print('Creating Spam({!r})'.format(name))
self.name = name
然后我也來測試一下:
>>> a = Spam('Guido')
Creating Spam('Guido')
>>> b = Spam('Diana')
Creating Spam('Diana')
>>> c = Spam('Guido') # Cached
>>> a is b
False
>>> a is c # Cached value returned
True
>>>
利用元類實(shí)現(xiàn)多種實(shí)例創(chuàng)建模式通常要比不使用元類的方式優(yōu)雅得多。
假設(shè)你不使用元類,你可能需要將類隱藏在某些工廠函數(shù)后面。比如為了實(shí)現(xiàn)一個(gè)單例,你你可能會像下面這樣寫:
class _Spam:
def __init__(self):
print('Creating Spam')
_spam_instance = None
def Spam():
global _spam_instance
if _spam_instance is not None:
return _spam_instance
else:
_spam_instance = _Spam()
return _spam_instance
盡管使用元類可能會涉及到比較高級點(diǎn)的技術(shù),但是它的代碼看起來會更加簡潔舒服,而且也更加直觀。
更多關(guān)于創(chuàng)建緩存實(shí)例、弱引用等內(nèi)容,請參考8.25小節(jié)。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: