NotImplemented 是一个非异常对象,NotImplementedError 是一个异常对象。

>>> NotImplemented
NotImplemented
>>> NotImplementedError
<type 'exceptions.NotImplementedError'> >>> type(NotImplemented)
<type 'NotImplementedType'>
>>> type(NotImplementedError)
<type 'type'>

  如果抛出 NotImplemented 会得到 TypeError,因为它不是一个异常。而抛出 NotImplementedError 会正常捕获该异常。

>>> raise NotImplemented

Traceback (most recent call last):
File "<pyshell#10>", line , in <module>
raise NotImplemented
TypeError: exceptions must be old-style classes or derived from BaseException, not NotImplementedType >>> raise NotImplementedError Traceback (most recent call last):
File "<pyshell#11>", line , in <module>
raise NotImplementedError
NotImplementedError

为什么要存在一个 NotImplemented 和一个 NotImplementedError 呢?

  

  在 Python 中对列表进行排序时,会经常间接使用像 __lt__() 这类比较运算的方法。

  有时 Python 的内部算法会选择别的方法来确定比较结果,或者直接选择一个默认的结果。如果抛出一个异常,则会打破排序运算,因此如果使用 NotImplemented 则不会抛出异常,这样 Python 可以尝试别的方法。

  NotImplemented 对象向运行时环境发出一个信号,告诉运行环境如果当前操作失败,它应该再检查一下其他可行方法。例如在 a == b 表达式,如果 a.__eq__(b) 返回 NotImplemented,那么 Python 会尝试 b.__eq__(a)。如果调用 b 的 __eq__() 方法可以返回 True 或者 False,那么该表达式就成功了。如果 b.__eq__(a) 也不能得出结果,那么 Python 会继续尝试其他方法,例如使用 != 来比较。  

参考一

[Python] NotImplemented 和 NotImplementedError 区别的更多相关文章

  1. python @classmethod和@staticmethod区别

    python 类方法和静态方法区别 python @classmethod和@staticmethod区别 Python中至少有三种比较常见的方法类型,即实例方法,类方法.静态方法.它们是如何定义的呢 ...

  2. 【python】classmethod & staticmethod 区别

    来源:http://blog.csdn.net/carolzhang8406/article/details/6856817 其他参考: http://blog.csdn.net/lovingprin ...

  3. python中@classmethod @staticmethod区别

    Python中3种方式定义类方法, 常规方式, @classmethod修饰方式, @staticmethod修饰方式. class A(object): def foo(self, x): prin ...

  4. python中@classmethod @staticmethod区别(转)

    pthon中3种方式定义类方法, 常规方式, @classmethod修饰方式, @staticmethod修饰方式. class A(object): def foo(self, x): print ...

  5. [转]python中@classmethod @staticmethod区别

    Python中3种方式定义类方法, 常规方式, @classmethod修饰方式, @staticmethod修饰方式. class A(object): def foo(self, x): prin ...

  6. python的==和is区别

    Python中: is判断两个标识符是否引自同一个对象 ==判断两个标识符的值是否相等 区别于java: ==判断两个标识符是否引自同一个对象 .equals()判断是否相等   #如果是String ...

  7. PyQt的QString和python的string的区别

    转载于http://blog.chinaunix.net/uid-200142-id-4018863.html python的string和PyQt的QString的区别 python string和 ...

  8. js中正则表达式与Python中正则表达式的区别

    今天女票让我帮她写一个js中的正则,来提取电话号码,对于正则规则来说,js与python是基本没有区别的,重点的区别是在一些函数与方法中. python中的正则提取: import re str = ...

  9. 2019-02-13 Python爬虫问题 NotImplementedError: Only the following pseudo-classes are implemented: nth-of-type.

    soup=BeautifulSoup(html.text,'lxml') #data=soup.select('body > div.main > div.ctr > div > ...

随机推荐

  1. FPGA中的时序分析(二)

    使用Timequest 笔者对Altera较熟悉,这里以quartus ii中的timequest作为讲解. Timequest分析时序的核心,也就是在于延迟因数的计算.那么建立约束文件,去告诉tim ...

  2. 关于Unity中FPS第一人称射击类游戏制作(专题十)

    当前Unity最新版本5.6.3f1,我使用的是5.5.1f1 场景搭建 1: 导入人物模型, 手持一把枪;2: 导入碎片模型;3: 创建一个平面;4: 创建一个障碍物;5: 导入人物模型;6: 配置 ...

  3. Linux 下查看某个进程运行的堆栈信息

    1. 根据进程名称查询进程ID ps -ef | grep processName 2. 将进程的堆栈信息写入log gstack processId > s.log 3. 查看log vim ...

  4. JAR 文件格式提供了许多优势和功能

    JAR 文件格式提供了许多优势和功能,其中很多是传统的压缩格式如 ZIP 或者 RAR 所没有提供的.它们包括: 安全性 可以对 JAR 文件内容加上数字化签名.这样,能够识别签名的工具就可以有选择地 ...

  5. pyqt5加载网路图片,不本地下载。

    依赖组件: requests class webImg: pass if __name__ == '__main__': import sys from PyQt5.QtWidgets import ...

  6. Spring Cloud 微服务的那点事

    什么是微服务 微服务的概念源于2014年3月Martin Fowler所写的一篇文章“Microservices”. 微服务架构是一种架构模式,它提倡将单一应用程序划分成一组小的服务,服务之间互相协调 ...

  7. 新建maven指定jdk版本-eclipse新建maven项目报错The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path的解决方案

    具体表现为: 使用Eclipse+Maven建立了一个Javaweb工程,JSP页面顶端出现“红色”的报错信息:The superclass "javax.servlet.http.Http ...

  8. ubuntu安装mongo数据库

    安装mongo数据库,在shell下输入 sudo apt-get install mongodb 如果需要在Python中使用mongo数据库,还需要额外安装Python封装库 pip instal ...

  9. Java标准I/O流编程一览笔录

    I/O是什么 I/O 是Input/Output(输入.输出)的简称,输入流可以理解为向内存输入,输出流是从内存输出. 流 流是一个连续的数据流,可以从流中读取数据,也可以往流中写数据.流与数据源,或 ...

  10. 利用neon技术对矩阵旋转进行加速

    一般的矩阵旋转操作都是对矩阵中的元素逐个操作,假设矩阵大小为m*n,那么时间复杂度就是o(mn).如果使用了arm公司提供的neon加速技术,则可以并行的读取多个元素,对多个元素进行操作,虽然时间复杂 ...