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. Sword STL之map效率问题

    #include <iostream> #include <map> #include <vector> using namespace std; /* STL容器 ...

  2. 自然语言交流系统 phxnet团队 创新实训 项目博客 (十四)

    项目技术借鉴报告: 一.服务器端(Server) 1.分词 分词使用的是庖丁分词.使用盘古分词词库和词素作为辅助.其中包括下载Jar包并且使用. 2.人工神经网络 以网上已有的初级网络为蓝图,结合机器 ...

  3. Debug 单步执行命令step into/step out/step over的区别

    总结一下在debug中三种调试的区别: step into就是单步执行,遇到子函数就进入并且继续单步执行: step over是在单步执行时,在函数内遇到子函数时不会进入子函数内单步执行,而是将子函数 ...

  4. Opengl绘制我们的小屋(三)纹理绘制

    本准备先说光照相关实现,但是发现对那个模型实在看不下去了,于是先绘制纹理. 先看下基本纹理贴上去的显示效果.具体模型图请看上篇文章的实现,这篇只讲纹理实现. 我们常见的纹理绘制差不多如下,先写一个纹理 ...

  5. 【转帖】Windows下PostgreSQL安装图解

    Windows下PostgreSQL安装图解     这篇文章主要为大家介绍了如果在Windows下安装PostgreSQL数据库的方法,需要的朋友可以参考下     现在谈起免费数据库,大多数人首先 ...

  6. Windows10锁屏壁纸提取

    CMD执行:  %localappdata%\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\As ...

  7. (原创)OpenStack服务如何使用Keystone(三)---详细配置Keystone中间件

    (一)Keystone端的操作 (二)如何在OpenStack服务上部署Keystone中间件 (三)详细配置keystonemiddleware 前文我们介绍了如何部署Keystone中间件以及中间 ...

  8. ELK filter过滤器来收集Nginx日志

    前面已经有ELK-Redis的安装,此处只讲在不改变日志格式的情况下收集Nginx日志. 1.Nginx端的日志格式设置如下: log_format access '$remote_addr - $r ...

  9. linux中service的问题

    1.描述问题 2.解决方案 systemctl stop firewalld systemctl mask firewalld Then, install the iptables-services ...

  10. spring + springMVC + spring Data + jpa + maven 项目框架搭建

    首先看一下项目结构: 所用到的jar(pom.xml): <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:x ...