一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - 詳解Python中內置的NotImplemented類型的用法

詳解Python中內置的NotImplemented類型的用法

2020-05-27 09:21Shahriar Tajbakhsh Python

這篇文章主要介紹了詳解Python中內置的NotImplemented類型的用法,包括對相關的__eq__()和__ne__()兩個方法使用的講解,需要的朋友可以參考下

它是什么?
 

?
1
2
>>> type(NotImplemented)
<type 'NotImplementedType'>

NotImplemented 是Python在內置命名空間中的六個常數之一。其他有False、True、None、Ellipsis 和 __debug__。和 Ellipsis很像,NotImplemented 能被重新賦值(覆蓋)。對它賦值,甚至改變屬性名稱, 不會產生 SyntaxError。所以它不是一個真正的“真”常數。當然,我們應該永遠不改變它。 但是為了完整性:
 

?
1
2
3
4
5
6
7
8
>>> None = 'hello'
...
SyntaxError: can't assign to keyword
>>> NotImplemented
NotImplemented
>>> NotImplemented = 'do not'
>>> NotImplemented
'do not'

它有什么用?什么時候用?

NotImplemented 是個特殊值,它能被二元特殊方法返回(比如__eq__() 、 __lt__()  、 __add__() 、 __rsub__() 等),表明某個類型沒有像其他類型那樣實現這些操作。同樣,它或許會被原地處理(in place)的二元特殊方法返回(比如__imul__()、__iand__()等)。還有,它的實際值為True:
 

?
1
2
>>> bool(NotImplemented)
True

你也許會問自己,“但我認為當這個操作沒有實現時,我應該產生個NotImpementedError”。我們會看些例子,關于為什么當實現二元特殊方法時不是這么回事兒。

讓我們看看NotImplemented常數的用法,通過__eq__()對于兩個非?;荆ㄇ覜]用)的類 A 和 B 的編碼。[對于這個簡單的例子,為了避免干擾,不會實現__ne__() ,但是總的說來,每次實現__eq__() 時, __ne__()也應該被實現,除非,有個足夠充分的理由去不實現它。]
 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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

現在,在解釋器中:
 

?
1
2
3
>>> from example import A, B
>>> a1 = A(1)
>>> b1 = B(1)

我們現在可以實驗下對于 __eq__() 不同的調用,看看發生了什么。作為提醒,在Python中,a == b會調用a.__eq__(b):
 

?
1
2
3
>>> a1 == a1
Comparing an A with an A
True

正如所望,a1等于a1(自己),使用類A中的__eq__()來進行這個比較的。比較b1和它自己也會產生類似結果:
 

?
1
2
3
>>> b1 == b1
Comparing a B with another B
True

現在,那要是我們比較a1和b1呢?由于在A的__eq__()會檢查other是不是B的一個實例,我們想要a1.__eq__(b1)去處理這個比較并返回True:
 

?
1
2
3
>>> a1 == b1
Comparing an A with a B
True

就是這樣。現在,如果我們比較b1和a1(即調用b1.__eq__(a1)),我們會想要返回NotImplemented。這是因為B的__eq__()只和其他B的實例進行比較。來看看發生了什么:
 

?
1
2
3
4
>>> b1 == a1
Could not compare B against the other class
Comparing an A with a B
True

聰明!b1.__eq__(a1)方法返回NotImplemented,這樣會導致調用A中的__eq__()方法。而且由于在A中的__eq__()定義了A和B之間的比較,所以就得到了正確的結果(True)。

這就是返回了NotImplemented的所做的。NotImplemented告訴運行時,應該讓其他對象來完成某個操作。在表達b1 == a1中,b1.__eq__(a1)返回了NotImplemented,這說明Python試著用a1.__eq__(b1)。由于a1足夠可以返回True,因此這個表達可以成功。如果A中的__eq__()也返回NotImplemented,那么運行時會退化到使用內置的比較行為,即比較對象的標識符(在CPython中,是對象在內存中的地址)。

注意:如果在調用b1.__eq__(a1)時拋出NotImpementedError,而不進行處理,就會中斷代碼的執行。而NotImplemented無法拋出,僅僅是用來進一步測試是否有其他方法可供調用。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产永久在线观看 | 国产免费一区二区 | 国产按摩系列 | 久久精品国产色蜜蜜麻豆国语版 | 国产精品乱码高清在线观看 | 男人猛进猛出女人下面视频 | 爱欲荡漾在线观看 | 91aaa免费免费国产在线观看 | 久久九九精品国产自在现线拍 | 日韩欧美亚洲每日更新网 | 免费视屏 | 国产成人www| 香蕉视频在线观看网站 | 91制片厂制作果冻传媒八夷 | 午夜爽喷水无码成人18禁三级 | 妹妹你插的我好爽 | 福利视频一区二区三区 | 俄罗斯15一16处交 | 91青青国产在线观看免费 | 亚洲高清毛片一区二区 | 风间由美被义子中文字幕 | 成人精品一区二区三区中文字幕 | 911香蕉视频 | 羞羞答答免费人成黄页在线观看国产 | 国产66 | 国产成人综合网亚洲欧美在线 | videojapan日本孕交孕 | 精品无码国产污污污免费网站2 | 久久这里只有精品视频9 | 无颜之月全集免费观看 | 给我免费观看的视频在线播放 | 福利国模私拍视频在线观看 | 美女扒开胸罩露出奶了无遮挡免费 | 国产成人久久久精品一区二区三区 | 91日本在线 | 国产在线视频色综合 | 韩国www| 四虎海外影院 | 久热人人综合人人九九精品视频 | 国产精品va在线观看不 | 97国产蝌蚪视频在线观看 |