W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
你想創(chuàng)建一個(gè)新的擁有一些額外功能的實(shí)例屬性類型,比如類型檢查。
如果你想創(chuàng)建一個(gè)全新的實(shí)例屬性,可以通過一個(gè)描述器類的形式來定義它的功能。下面是一個(gè)例子:
# Descriptor attribute for an integer type-checked attribute
class Integer:
def __init__(self, name):
self.name = name
def __get__(self, instance, cls):
if instance is None:
return self
else:
return instance.__dict__[self.name]
def __set__(self, instance, value):
if not isinstance(value, int):
raise TypeError('Expected an int')
instance.__dict__[self.name] = value
def __delete__(self, instance):
del instance.__dict__[self.name]
一個(gè)描述器就是一個(gè)實(shí)現(xiàn)了三個(gè)核心的屬性訪問操作(get, set, delete)的類,分別為 __get__()
、__set__()
和 __delete__()
這三個(gè)特殊的方法。這些方法接受一個(gè)實(shí)例作為輸入,之后相應(yīng)的操作實(shí)例底層的字典。
為了使用一個(gè)描述器,需將這個(gè)描述器的實(shí)例作為類屬性放到一個(gè)類的定義中。例如:
class Point:
x = Integer('x')
y = Integer('y')
def __init__(self, x, y):
self.x = x
self.y = y
當(dāng)你這樣做后,所有隊(duì)描述器屬性(比如x或y)的訪問會(huì)被__get__()
、__set__()
和 __delete__()
方法捕獲到。例如:
>>> p = Point(2, 3)
>>> p.x # Calls Point.x.__get__(p,Point)
2
>>> p.y = 5 # Calls Point.y.__set__(p, 5)
>>> p.x = 2.3 # Calls Point.x.__set__(p, 2.3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "descrip.py", line 12, in __set__
raise TypeError('Expected an int')
TypeError: Expected an int
>>>
作為輸入,描述器的每一個(gè)方法會(huì)接受一個(gè)操作實(shí)例。為了實(shí)現(xiàn)請(qǐng)求操作,會(huì)相應(yīng)的操作實(shí)例底層的字典(dict屬性)。描述器的 self.name
屬性存儲(chǔ)了在實(shí)例字典中被實(shí)際使用到的key。
描述器可實(shí)現(xiàn)大部分Python類特性中的底層魔法,包括 @classmethod
、@staticmethod
、@property
,甚至是 __slots__
特性。
通過定義一個(gè)描述器,你可以在底層捕獲核心的實(shí)例操作(get, set, delete),并且可完全自定義它們的行為。這是一個(gè)強(qiáng)大的工具,有了它你可以實(shí)現(xiàn)很多高級(jí)功能,并且它也是很多高級(jí)庫和框架中的重要工具之一。
描述器的一個(gè)比較困惑的地方是它只能在類級(jí)別被定義,而不能為每個(gè)實(shí)例單獨(dú)定義。因此,下面的代碼是無法工作的:
# Does NOT work
class Point:
def __init__(self, x, y):
self.x = Integer('x') # No! Must be a class variable
self.y = Integer('y')
self.x = x
self.y = y
同時(shí),__get__()
方法實(shí)現(xiàn)起來比看上去要復(fù)雜得多:
# Descriptor attribute for an integer type-checked attribute
class Integer:
def __get__(self, instance, cls):
if instance is None:
return self
else:
return instance.__dict__[self.name]
__get__()
看上去有點(diǎn)復(fù)雜的原因歸結(jié)于實(shí)例變量和類變量的不同。如果一個(gè)描述器被當(dāng)做一個(gè)類變量來訪問,那么 instance
參數(shù)被設(shè)置成 None
。這種情況下,標(biāo)準(zhǔn)做法就是簡單的返回這個(gè)描述器本身即可(盡管你還可以添加其他的自定義操作)。例如:
>>> p = Point(2,3)
>>> p.x # Calls Point.x.__get__(p, Point)
2
>>> Point.x # Calls Point.x.__get__(None, Point)
<__main__.Integer object at 0x100671890>
>>>
描述器通常是那些使用到裝飾器或元類的大型框架中的一個(gè)組件。同時(shí)它們的使用也被隱藏在后面。舉個(gè)例子,下面是一些更高級(jí)的基于描述器的代碼,并涉及到一個(gè)類裝飾器:
# Descriptor for a type-checked attribute
class Typed:
def __init__(self, name, expected_type):
self.name = name
self.expected_type = expected_type
def __get__(self, instance, cls):
if instance is None:
return self
else:
return instance.__dict__[self.name]
def __set__(self, instance, value):
if not isinstance(value, self.expected_type):
raise TypeError('Expected ' + str(self.expected_type))
instance.__dict__[self.name] = value
def __delete__(self, instance):
del instance.__dict__[self.name]
# Class decorator that applies it to selected attributes
def typeassert(**kwargs):
def decorate(cls):
for name, expected_type in kwargs.items():
# Attach a Typed descriptor to the class
setattr(cls, name, Typed(name, expected_type))
return cls
return decorate
# Example use
@typeassert(name=str, shares=int, price=float)
class Stock:
def __init__(self, name, shares, price):
self.name = name
self.shares = shares
self.price = price
最后要指出的一點(diǎn)是,如果你只是想簡單的自定義某個(gè)類的單個(gè)屬性訪問的話就不用去寫描述器了。這種情況下使用8.6小節(jié)介紹的property技術(shù)會(huì)更加容易。當(dāng)程序中有很多重復(fù)代碼的時(shí)候描述器就很有用了(比如你想在你代碼的很多地方使用描述器提供的功能或者將它作為一個(gè)函數(shù)庫特性)。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: