詳解Python中內(nèi)置的NotImplemented類型的用法
它是什么?
>>> type(NotImplemented) <type 'NotImplementedType'>
NotImplemented 是Python在內(nèi)置命名空間中的六個常數(shù)之一。其他有False、True、None、Ellipsis 和 __debug__。和 Ellipsis很像,NotImplemented 能被重新賦值(覆蓋)。對它賦值,甚至改變屬性名稱, 不會產(chǎn)生 SyntaxError。所以它不是一個真正的“真”常數(shù)。當(dāng)然,我們應(yīng)該永遠(yuǎn)不改變它。 但是為了完整性:
>>> None = 'hello' ... SyntaxError: can't assign to keyword >>> NotImplemented NotImplemented >>> NotImplemented = 'do not' >>> NotImplemented 'do not'
它有什么用?什么時候用?
NotImplemented 是個特殊值,它能被二元特殊方法返回(比如__eq__() 、 __lt__() 、 __add__() 、 __rsub__() 等),表明某個類型沒有像其他類型那樣實現(xiàn)這些操作。同樣,它或許會被原地處理(in place)的二元特殊方法返回(比如__imul__()、__iand__()等)。還有,它的實際值為True:
>>> bool(NotImplemented) True
你也許會問自己,“但我認(rèn)為當(dāng)這個操作沒有實現(xiàn)時,我應(yīng)該產(chǎn)生個NotImpementedError”。我們會看些例子,關(guān)于為什么當(dāng)實現(xiàn)二元特殊方法時不是這么回事兒。
讓我們看看NotImplemented常數(shù)的用法,通過__eq__()對于兩個非?;荆ㄇ覜]用)的類 A 和 B 的編碼。[對于這個簡單的例子,為了避免干擾,不會實現(xiàn)__ne__() ,但是總的說來,每次實現(xiàn)__eq__() 時, __ne__()也應(yīng)該被實現(xiàn),除非,有個足夠充分的理由去不實現(xiàn)它。]
# example.py class A(object): def __init__(self, value): self.value = value def __eq__(self, other): if isinstance(other, A): print('Comparing an A with an A') return other.value == self.value if isinstance(other, B): print('Comparing an A with a B') return other.value == self.value print('Could not compare A with the other class') return NotImplemented class B(object): def __init__(self, value): self.value = value def __eq__(self, other): if isinstance(other, B): print('Comparing a B with another B') return other.value == self.value print('Could not compare B with the other class') return NotImplemented
現(xiàn)在,在解釋器中:
>>> from example import A, B >>> a1 = A(1) >>> b1 = B(1)
我們現(xiàn)在可以實驗下對于 __eq__() 不同的調(diào)用,看看發(fā)生了什么。作為提醒,在Python中,a == b會調(diào)用a.__eq__(b):
>>> a1 == a1 Comparing an A with an A True
正如所望,a1等于a1(自己),使用類A中的__eq__()來進(jìn)行這個比較的。比較b1和它自己也會產(chǎn)生類似結(jié)果:
>>> b1 == b1 Comparing a B with another B True
現(xiàn)在,那要是我們比較a1和b1呢?由于在A的__eq__()會檢查other是不是B的一個實例,我們想要a1.__eq__(b1)去處理這個比較并返回True:
>>> a1 == b1 Comparing an A with a B True
就是這樣?,F(xiàn)在,如果我們比較b1和a1(即調(diào)用b1.__eq__(a1)),我們會想要返回NotImplemented。這是因為B的__eq__()只和其他B的實例進(jìn)行比較。來看看發(fā)生了什么:
>>> b1 == a1 Could not compare B against the other class Comparing an A with a B True
聰明!b1.__eq__(a1)方法返回NotImplemented,這樣會導(dǎo)致調(diào)用A中的__eq__()方法。而且由于在A中的__eq__()定義了A和B之間的比較,所以就得到了正確的結(jié)果(True)。
這就是返回了NotImplemented的所做的。NotImplemented告訴運行時,應(yīng)該讓其他對象來完成某個操作。在表達(dá)b1 == a1中,b1.__eq__(a1)返回了NotImplemented,這說明Python試著用a1.__eq__(b1)。由于a1足夠可以返回True,因此這個表達(dá)可以成功。如果A中的__eq__()也返回NotImplemented,那么運行時會退化到使用內(nèi)置的比較行為,即比較對象的標(biāo)識符(在CPython中,是對象在內(nèi)存中的地址)。
注意:如果在調(diào)用b1.__eq__(a1)時拋出NotImpementedError,而不進(jìn)行處理,就會中斷代碼的執(zhí)行。而NotImplemented無法拋出,僅僅是用來進(jìn)一步測試是否有其他方法可供調(diào)用。
相關(guān)文章
pytorch實現(xiàn)梯度下降和反向傳播圖文詳細(xì)講解
這篇文章主要介紹了pytorch實現(xiàn)梯度下降和反向傳播,反向傳播的目的是計算成本函數(shù)C對網(wǎng)絡(luò)中任意w或b的偏導(dǎo)數(shù)。一旦我們有了這些偏導(dǎo)數(shù),我們將通過一些常數(shù)α的乘積和該數(shù)量相對于成本函數(shù)的偏導(dǎo)數(shù)來更新網(wǎng)絡(luò)中的權(quán)重和偏差2023-04-04Python計算三角函數(shù)之a(chǎn)sin()方法的使用
這篇文章主要介紹了Python計算三角函數(shù)之a(chǎn)sin()方法的使用,是Python入門的基礎(chǔ)知識,需要的朋友可以參考下2015-05-05