這個 Python 知識點,90% 的人都得掛~
學(xué)習(xí) Python 這么久了,說起 Python 的優(yōu)雅之處,能讓我脫口而出的, Descriptor(描述符)特性可以排得上號。
描述符 是Python 語言獨有的特性,它不僅在應(yīng)用層使用,在語言語法糖的實現(xiàn)上也有使用到(在下面的文章會一一介紹)。
當(dāng)你點進(jìn)這篇文章時
-
你也許沒學(xué)過描述符,甚至沒聽過描述符。
-
或者你對描述符只是一知半解
無論你是哪種,本篇都將帶你全面的學(xué)習(xí)描述符,一起來感受 Python 語言的優(yōu)雅。
1. 為什么要使用描述符?
假想你正在給學(xué)校寫一個成績管理系統(tǒng),并沒有太多編碼經(jīng)驗的你,可能會這樣子寫。
- class Student:
- def __init__(self, name, math, chinese, english):
- self.name = name
- self.math = math
- self.chinese = chinese
- self.english = english
- def __repr__(self):
- return "<Student: {}, math:{}, chinese: {}, english:{}>".format(
- self.name, self.math, self.chinese, self.english
- )
看起來一切都很合理
- >>> std1 = Student('小明', 76, 87, 68)
- >>> std1
- <Student: 小明, math:76, chinese: 87, english:68>
但是程序并不像人那么智能,不會自動根據(jù)使用場景判斷數(shù)據(jù)的合法性,如果老師在錄入成績的時候,不小心錄入了將成績錄成了負(fù)數(shù),或者超過100,程序是無法感知的。
聰明的你,馬上在代碼中加入了判斷邏輯。
- class Student:
- def __init__(self, name, math, chinese, english):
- self.name = name
- if 0 <= math <= 100:
- self.math = math
- else:
- raise ValueError("Valid value must be in [0, 100]")
- if 0 <= chinese <= 100:
- self.chinese = chinese
- else:
- raise ValueError("Valid value must be in [0, 100]")
- if 0 <= chinese <= 100:
- self.english = english
- else:
- raise ValueError("Valid value must be in [0, 100]")
- def __repr__(self):
- return "<Student: {}, math:{}, chinese: {}, english:{}>".format(
- self.name, self.math, self.chinese, self.english
- )
這下程序稍微有點人工智能了,能夠自己明辨是非了。
程序是智能了,但在 __init__
里有太多的判斷邏輯,很影響代碼的可讀性。巧的是,你剛好學(xué)過 Property 特性,可以很好的應(yīng)用在這里。于是你將代碼修改成如下,代碼的可讀性瞬間提升了不少
- class Student:
- def __init__(self, name, math, chinese, english):
- self.name = name
- self.math = math
- self.chinese = chinese
- self.english = english
- @property
- def math(self):
- return self._math
- @math.setter
- def math(self, value):
- if 0 <= value <= 100:
- self._math = value
- else:
- raise ValueError("Valid value must be in [0, 100]")
- @property
- def chinese(self):
- return self._chinese
- @chinese.setter
- def chinese(self, value):
- if 0 <= value <= 100:
- self._chinese = value
- else:
- raise ValueError("Valid value must be in [0, 100]")
- @property
- def english(self):
- return self._english
- @english.setter
- def english(self, value):
- if 0 <= value <= 100:
- self._english = value
- else:
- raise ValueError("Valid value must be in [0, 100]")
- def __repr__(self):
- return "<Student: {}, math:{}, chinese: {}, english:{}>".format(
- self.name, self.math, self.chinese, self.english
- )
程序還是一樣的人工智能,非常好。
你以為你寫的代碼,已經(jīng)非常優(yōu)秀,無懈可擊了。
沒想到,人外有天,你的主管看了你的代碼后,深深地嘆了口氣:類里的三個屬性,math、chinese、english,都使用了 Property 對屬性的合法性進(jìn)行了有效控制。功能上,沒有問題,但就是太啰嗦了,三個變量的合法性邏輯都是一樣的,只要大于0,小于100 就可以,代碼重復(fù)率太高了,這里三個成績還好,但假設(shè)還有地理、生物、歷史、化學(xué)等十幾門的成績呢,這代碼簡直沒法忍。去了解一下 Python 的描述符吧。
經(jīng)過主管的指點,你知道了「描述符」這個東西。懷著一顆敬畏之心,你去搜索了下關(guān)于 描述符的用法。
其實也很簡單,一個實現(xiàn)了 描述符協(xié)議
的類就是一個描述符。
什么描述符協(xié)議:在類里實現(xiàn)了 __get__()
、 __set__()
、 __delete__()
其中至少一個方法。
__get__
:用于訪問屬性。它返回屬性的值,若屬性不存在、不合法等都可以拋出對應(yīng)的異常。__set__
:將在屬性分配操作中調(diào)用。不會返回任何內(nèi)容。__delete__
:控制刪除操作。不會返回內(nèi)容。
對描述符有了大概的了解后,你開始重寫上面的方法。
如前所述,Score 類是一個描述符,當(dāng)從 Student 的實例訪問 math、chinese、english這三個屬性的時候,都會經(jīng)過 Score 類里的三個特殊的方法。這里的 Score 避免了 使用Property 出現(xiàn)大量的代碼無法復(fù)用的尷尬。
- class Score:
- def __init__(self, default=0):
- self._score = default
- def __set__(self, instance, value):
- if not isinstance(value, int):
- raise TypeError('Score must be integer')
- if not 0 <= value <= 100:
- raise ValueError('Valid value must be in [0, 100]')
- self._score = value
- def __get__(self, instance, owner):
- return self._score
- def __delete__(self):
- del self._score
- class Student:
- math = Score(0)
- chinese = Score(0)
- english = Score(0)
- def __init__(self, name, math, chinese, english):
- self.name = name
- self.math = math
- self.chinese = chinese
- self.english = english
- def __repr__(self):
- return "<Student: {}, math:{}, chinese: {}, english:{}>".format(
- self.name, self.math, self.chinese, self.english
- )
實現(xiàn)的效果和前面的一樣,可以對數(shù)據(jù)的合法性進(jìn)行有效控制(字段類型、數(shù)值區(qū)間等)
以上,我舉了下具體的實例,從最原始的編碼風(fēng)格到 Property ,最后引出描述符。由淺入深,一步一步帶你感受到描述符的優(yōu)雅之處。
到這里,你需要記住的只有一點,就是描述符給我們帶來的編碼上的便利,它在實現(xiàn) 保護(hù)屬性不受修改
、 屬性類型檢查
的基本功能,同時有大大提高代碼的復(fù)用率。
2. 描述符的訪問規(guī)則
描述符分兩種:
- 數(shù)據(jù)描述符:實現(xiàn)了
__get__
和__set__
兩種方法的描述符 - 非數(shù)據(jù)描述符:只實現(xiàn)了
__get__
一種方法的描述符
你一定會問,他們有什么區(qū)別呢?網(wǎng)上的講解,我看過幾個,很多都把一個簡單的東西講得復(fù)雜了。
其實就一句話, 數(shù)據(jù)描述器和非數(shù)據(jù)描述器的區(qū)別在于:它們相對于實例的字典的優(yōu)先級不同。
如果實例字典中有與描述符同名的屬性,如果描述符是數(shù)據(jù)描述符,優(yōu)先使用數(shù)據(jù)描述符,如果是非數(shù)據(jù)描述符,優(yōu)先使用字典中的屬性。
這邊還是以上節(jié)的成績管理的例子來說明,方便你理解。
- # 數(shù)據(jù)描述符
- class DataDes:
- def __init__(self, default=0):
- self._score = default
- def __set__(self, instance, value):
- self._score = value
- def __get__(self, instance, owner):
- print("訪問數(shù)據(jù)描述符里的 __get__")
- return self._score
- # 非數(shù)據(jù)描述符
- class NoDataDes:
- def __init__(self, default=0):
- self._score = default
- def __get__(self, instance, owner):
- print("訪問非數(shù)據(jù)描述符里的 __get__")
- return self._score
- class Student:
- math = DataDes(0)
- chinese = NoDataDes(0)
- def __init__(self, name, math, chinese):
- self.name = name
- self.math = math
- self.chinese = chinese
- def __getattribute__(self, item):
- print("調(diào)用 __getattribute__")
- return super(Student, self).__getattribute__(item)
- def __repr__(self):
- return "<Student: {}, math:{}, chinese: {},>".format(
- self.name, self.math, self.chinese)
需要注意的是,math 是數(shù)據(jù)描述符,而 chinese 是非數(shù)據(jù)描述符。從下面的驗證中,可以看出,當(dāng)實例屬性和數(shù)據(jù)描述符同名時,會優(yōu)先訪問數(shù)據(jù)描述符(如下面的math),而當(dāng)實例屬性和非數(shù)據(jù)描述符同名時,會優(yōu)先訪問實例屬性( __getattribute__
)
- >>> std = Student('xm', 88, 99)
- >>>
- >>> std.math
- 調(diào)用 __getattribute__
- 訪問數(shù)據(jù)描述符里的 __get__
- 88
- >>> std.chinese
- 調(diào)用 __getattribute__
- 99
講完了數(shù)據(jù)描述符和非數(shù)據(jù)描述符,我們還需要了解的對象屬性的查找規(guī)律。
當(dāng)我們對一個實例屬性進(jìn)行訪問時,Python 會按 obj.__dict__
→ type(obj).__dict__
→ type(obj)的父類.__dict__
順序進(jìn)行查找,如果查找到目標(biāo)屬性并發(fā)現(xiàn)是一個描述符,Python 會調(diào)用描述符協(xié)議來改變默認(rèn)的控制行為。
3. 基于描述符如何實現(xiàn)property
經(jīng)過上面的講解,我們已經(jīng)知道如何定義描述符,且明白了描述符是如何工作的。
正常人所見過的描述符的用法就是上面提到的那些,我想說的是那只是描述符協(xié)議最常見的應(yīng)用之一,或許你還不知道,其實有很多 Python 的特性的底層實現(xiàn)機制都是基于 描述符協(xié)議
的,比如我們熟悉的 @property
、 @classmethod
、 @staticmethod
和 super
等。
先來說說 property
吧。
有了前面的基礎(chǔ),我們知道了 property 的基本用法。這里我直接切入主題,從第一篇的例子里精簡了一下。
- class Student:
- def __init__(self, name):
- self.name = name
- @property
- def math(self):
- return self._math
- @math.setter
- def math(self, value):
- if 0 <= value <= 100:
- self._math = value
- else:
- raise ValueError("Valid value must be in [0, 100]")
不防再簡單回顧一下它的用法,通過property裝飾的函數(shù),如例子中的 math 會變成 Student 實例的屬性。而對 math 屬性賦值會進(jìn)入 使用 math.setter
裝飾函數(shù)的邏輯代碼塊。
為什么說 property 底層是基于描述符協(xié)議的呢?通過 PyCharm 點擊進(jìn)入 property 的源碼,很可惜,只是一份類似文檔一樣的偽源碼,并沒有其具體的實現(xiàn)邏輯。
不過,從這份偽源碼的魔法函數(shù)結(jié)構(gòu)組成,可以大體知道其實現(xiàn)邏輯。
這里我自己通過模仿其函數(shù)結(jié)構(gòu),結(jié)合「描述符協(xié)議」來自己實現(xiàn)類 property
特性。
代碼如下:
- class TestProperty(object):
- def __init__(self, fget=None, fset=None, fdel=None, doc=None):
- self.fget = fget
- self.fset = fset
- self.fdel = fdel
- self.__doc__ = doc
- def __get__(self, obj, objtype=None):
- print("in __get__")
- if obj is None:
- return self
- if self.fget is None:
- raise AttributeError
- return self.fget(obj)
- def __set__(self, obj, value):
- print("in __set__")
- if self.fset is None:
- raise AttributeError
- self.fset(obj, value)
- def __delete__(self, obj):
- print("in __delete__")
- if self.fdel is None:
- raise AttributeError
- self.fdel(obj)
- def getter(self, fget):
- print("in getter")
- return type(self)(fget, self.fset, self.fdel, self.__doc__)
- def setter(self, fset):
- print("in setter")
- return type(self)(self.fget, fset, self.fdel, self.__doc__)
- def deleter(self, fdel):
- print("in deleter")
- return type(self)(self.fget, self.fset, fdel, self.__doc__)
然后 Student 類,我們也相應(yīng)改成如下
- class Student:
- def __init__(self, name):
- self.name = name
- # 其實只有這里改變
- @TestProperty
- def math(self):
- return self._math
- @math.setter
- def math(self, value):
- if 0 <= value <= 100:
- self._math = value
- else:
- raise ValueError("Valid value must be in [0, 100]")
為了盡量讓你少產(chǎn)生一點疑惑,我這里做兩點說明:
-
使用
TestProperty
裝飾后,math
不再是一個函數(shù),而是TestProperty
類的一個實例。所以第二個math函數(shù)可以使用math.setter
來裝飾,本質(zhì)是調(diào)用TestProperty.setter
來產(chǎn)生一個新的TestProperty
實例賦值給第二個math
。 -
第一個
math
和第二個math
是兩個不同TestProperty
實例。但他們都屬于同一個描述符類(TestProperty),當(dāng)對 math 對于賦值時,就會進(jìn)入TestProperty.__set__
,當(dāng)對math 進(jìn)行取值里,就會進(jìn)入TestProperty.__get__
。仔細(xì)一看,其實最終訪問的還是Student實例的_math
屬性。
說了這么多,還是運行一下,更加直觀一點。
- # 運行后,會直接打印這一行,這是在實例化 TestProperty 并賦值給第二個math
- in setter
- >>>
- >>> s1.math = 90
- in __set__
- >>> s1.math
- in __get__
- 90
對于以上理解 property
的運行原理有困難的同學(xué),請務(wù)必參照我上面寫的兩點說明。如有其他疑問,可以加微信與我進(jìn)行探討。
4. 基于描述符如何實現(xiàn)staticmethod
說完了 property
,這里再來講講 @classmethod
和 @staticmethod
的實現(xiàn)原理。
我這里定義了一個類,用了兩種方式來實現(xiàn)靜態(tài)方法。
- class Test:
- @staticmethod
- def myfunc():
- print("hello")
- # 上下兩種寫法等價
- class Test:
- def myfunc():
- print("hello")
- # 重點:這就是描述符的體現(xiàn)
- myfunc = staticmethod(myfunc)
這兩種寫法是等價的,就好像在 property
一樣,其實以下兩種寫法也是等價的。
- @TestProperty
- def math(self):
- return self._math
- math = TestProperty(fget=math)
話題還是轉(zhuǎn)回到 staticmethod
這邊來吧。
由上面的注釋,可以看出 staticmethod
其實就相當(dāng)于一個描述符類,而 myfunc
在此刻變成了一個描述符。關(guān)于 staticmethod
的實現(xiàn),你可以參照下面這段我自己寫的代碼,加以理解。
調(diào)用這個方法可以知道,每調(diào)用一次,它都會經(jīng)過描述符類的 __get__
。
- >>> Test.myfunc()
- in staticmethod __get__
- hello
- >>> Test().myfunc()
- in staticmethod __get__
- hello
5. 基于描述符如何實現(xiàn)classmethod
同樣的 classmethod
也是一樣。
- class classmethod(object):
- def __init__(self, f):
- self.f = f
- def __get__(self, instance, owner=None):
- print("in classmethod __get__")
- def newfunc(*args):
- return self.f(owner, *args)
- return newfunc
- class Test:
- def myfunc(cls):
- print("hello")
- # 重點:這就是描述符的體現(xiàn)
- myfunc = classmethod(myfunc)
驗證結(jié)果如下
- >>> Test.myfunc()
- in classmethod __get__
- hello
- >>> Test().myfunc()
- in classmethod __get__
- hello
講完了 property
、 staticmethod
和 classmethod
與 描述符的關(guān)系。我想你應(yīng)該對描述符在 Python 中的應(yīng)用有了更深的理解。對于 super 的實現(xiàn)原理,就交由你來自己完成。
6. 所有實例共享描述符
通過以上內(nèi)容的學(xué)習(xí),你是不是覺得自己已經(jīng)對描述符足夠了解了呢?
可在這里,我想說以上的描述符代碼都有問題。
問題在哪里呢?請看下面這個例子。
- class Score:
- def __init__(self, default=0):
- self._value = default
- def __get__(self, instance, owner):
- return self._value
- def __set__(self, instance, value):
- if 0 <= value <= 100:
- self._value = value
- else:
- raise ValueError
- class Student:
- math = Score(0)
- chinese = Score(0)
- english = Score(0)
- def __repr__(self):
- return "<Student math:{}, chinese:{}, english:{}>".format(self.math, self.chinese, self.english)
Student 里沒有像前面那樣寫了構(gòu)造函數(shù),但是關(guān)鍵不在這兒,沒寫只是因為沒必要寫。
然后來看一下會出現(xiàn)什么樣的問題呢
- >>> std1 = Student()
- >>> std1
- <Student math:0, chinese:0, english:0>
- >>> std1.math = 85
- >>> std1
- <Student math:85, chinese:0, english:0>
- >>> std2 = Student()
- >>> std2 # std2 居然共享了std1 的屬性值
- <Student math:85, chinese:0, english:0>
- >>> std2.math = 100
- >>> std1 # std2 也會改變std1 的屬性值
- <Student math:100, chinese:0, english:0>
從結(jié)果上來看,std2 居然共享了 std1 的屬性值,只要其中一個實例的變量發(fā)生改變,另一個實例的變量也會跟著改變。
探其根因,是由于此時 math,chinese,english 三個全部是類變量,導(dǎo)致 std2 和 std1 在訪問 math,chinese,english 這三個變量時,其實都是訪問類變量。
問題是不是來了?小明和小強的分?jǐn)?shù)怎么可能是綁定的呢?這很明顯與實際業(yè)務(wù)不符。
使用描述符給我們制造了便利,卻無形中給我們帶來了麻煩,難道這也是描述符的特性嗎?
描述符是個很好用的特性,會出現(xiàn)這個問題,是由于我們之前寫的描述符代碼都是錯誤的。
描述符的機制,在我看來,只是搶占了訪問順序,而具體的邏輯卻要因地制宜,視情況而定。
如果要把 math,chinese,english 這三個變量變成實例之間相互隔離的屬性,應(yīng)該這么寫。
- class Score:
- def __init__(self, subject):
- self.name = subject
- def __get__(self, instance, owner):
- return instance.__dict__[self.name]
- def __set__(self, instance, value):
- if 0 <= value <= 100:
- instance.__dict__[self.name] = value
- else:
- raise ValueError
- class Student:
- math = Score("math")
- chinese = Score("chinese")
- english = Score("english")
- def __init__(self, math, chinese, english):
- self.math = math
- self.chinese = chinese
- self.english = english
- def __repr__(self):
- return "<Student math:{}, chinese:{}, english:{}>".format(self.math, self.chinese, self.english)
引導(dǎo)程序邏輯進(jìn)入描述符之后,不管你是獲取屬性,還是設(shè)置屬性,都是直接作用于 instance 的。
這段代碼,你可以仔細(xì)和前面的對比一下。
不難看出:
-
之前的錯誤代碼,更像是把描述符當(dāng)做了存儲節(jié)點。
-
之后的正確代碼,則是把描述符直接當(dāng)做代理,本身不存儲值。
以上便是我對描述符的全部分享,希望能對你有所幫助。