網(wǎng)站首頁 編程語言 正文
一、NotImplemented它是什么?
>>> type(NotImplemented)
NotImplemented
是Python
在內(nèi)置命名空間中的六個(gè)常數(shù)之一。其他有False
、True
、None
、Ellipsis
和 debug
。和 Ellipsis很像,[NotImplemented
] 能被重新賦值(覆蓋)。對(duì)它賦值,甚至改變屬性名稱, 不會(huì)產(chǎn)生 SyntaxError。所以它不是一個(gè)真正的“真”常數(shù)。當(dāng)然,我們應(yīng)該永遠(yuǎn)不改變它。
但是為了完整性:
>>> None = 'hello' ... SyntaxError: can't assign to keyword >>> NotImplemented NotImplemented >>> NotImplemented = 'do not' >>> NotImplemented 'do not'
二、它有什么用?什么時(shí)候用?
NotImplemented
是個(gè)特殊值,它能被二元特殊方法返回(比如__eq__()、 lt() 、 add() 、 rsub() 等),表明某個(gè)類型沒有像其他類型那樣實(shí)現(xiàn)這些操作。同樣,它或許會(huì)被原地處理(in place)的二元特殊方法返回(比如__imul__()、iand()等)。
還有,它的實(shí)際值為True:
>>> bool(NotImplemented) True
你也許會(huì)問自己,“但我認(rèn)為當(dāng)這個(gè)操作沒有實(shí)現(xiàn)時(shí),我應(yīng)該產(chǎn)生個(gè)NotImpementedError
”。我們會(huì)看些例子,關(guān)于為什么當(dāng)實(shí)現(xiàn)二元特殊方法時(shí)不是這么回事兒。
讓我們看看NotImplemented
常數(shù)的用法,通過__eq__()
對(duì)于兩個(gè)非?;荆ㄇ覜]用)的類 A 和 B 的編碼。對(duì)于這個(gè)簡單的例子,為了避免干擾,不會(huì)實(shí)現(xiàn)__ne__()
,但是總的說來,每次實(shí)現(xiàn)__eq__() 時(shí), ne()也應(yīng)該被實(shí)現(xiàn),除非,有個(gè)足夠充分的理由去不實(shí)現(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)在可以實(shí)驗(yàn)下對(duì)于 eq() 不同的調(diào)用,看看發(fā)生了什么。
作為提醒,在Python中,a == b會(huì)調(diào)用a.eq(b):
>>> a1 == a1 Comparing an A with an A True
正如所望,a1等于a1(自己),使用類A中的__eq__()來進(jìn)行這個(gè)比較的。
比較b1和它自己也會(huì)產(chǎn)生類似結(jié)果:
>>> b1 == b1 Comparing a B with another B True
現(xiàn)在,那要是我們比較a1和b1呢?由于在A的__eq__()
會(huì)檢查other是不是B的一個(gè)實(shí)例,我們想要a1.eq(b1)去處理這個(gè)比較并返回True:
>>> a1 == b1 Comparing an A with a B True
就是這樣。現(xiàn)在,如果我們比較b1和a1(即調(diào)用b1.eq(a1)),我們會(huì)想要返回NotImplemented。這是因?yàn)锽的__eq__()只和其他B的實(shí)例進(jìn)行比較。
來看看發(fā)生了什么:
>>> b1 == a1 Could not compare B against the other class Comparing an A with a B True
聰明!b1.eq(a1)方法返回NotImplemented
,這樣會(huì)導(dǎo)致調(diào)用A中的__eq__()
方法。而且由于在A中的__eq__()定義了A和B之間的比較,所以就得到了正確的結(jié)果(True)。
這就是返回了NotImplemented
的所做的。NotImplemented告訴運(yùn)行時(shí),應(yīng)該讓其他對(duì)象來完成某個(gè)操作。在表達(dá)b1 == a1中,b1.eq(a1)返回了NotImplemented,這說明Python試著用a1.eq(b1)。由于a1足夠可以返回True,因此這個(gè)表達(dá)可以成功。如果A中的__eq__()也返回NotImplemented,那么運(yùn)行時(shí)會(huì)退化到使用內(nèi)置的比較行為,即比較對(duì)象的標(biāo)識(shí)符(在CPython中,是對(duì)象在內(nèi)存中的地址)。
注意:如果在調(diào)用b1.eq(a1)時(shí)拋出NotImpementedError,而不進(jìn)行處理,就會(huì)中斷代碼的執(zhí)行。而NotImplemented無法拋出,僅僅是用來進(jìn)一步測(cè)試是否有其他方法可供調(diào)用。
原文鏈接:https://blog.csdn.net/sinat_38682860/article/details/98469803
相關(guān)推薦
- 2022-03-30 Docker搭建RabbitMQ集群的方法步驟_docker
- 2022-06-12 批處理命令批量復(fù)制文件并重命名的實(shí)現(xiàn)_DOS/BAT
- 2022-11-19 通過?C#/VB.NET?代碼將?Excel?工作表拆分為單獨(dú)的文件_C#教程
- 2022-04-11 【Android,kotlin】寫倒計(jì)時(shí)CountDown的正確姿勢(shì)
- 2022-07-16 Electron項(xiàng)目中的NSIS配置項(xiàng)
- 2022-06-04 python對(duì)文檔中元素刪除,替換操作_python
- 2022-03-19 K8S中五種控制器的介紹以及使用_云其它
- 2022-09-13 python開發(fā)sdk模塊的方法_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支