[Python] NotImplemented 和 NotImplementedError 区别
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 区别的更多相关文章
- python @classmethod和@staticmethod区别
python 类方法和静态方法区别 python @classmethod和@staticmethod区别 Python中至少有三种比较常见的方法类型,即实例方法,类方法.静态方法.它们是如何定义的呢 ...
- 【python】classmethod & staticmethod 区别
来源:http://blog.csdn.net/carolzhang8406/article/details/6856817 其他参考: http://blog.csdn.net/lovingprin ...
- python中@classmethod @staticmethod区别
Python中3种方式定义类方法, 常规方式, @classmethod修饰方式, @staticmethod修饰方式. class A(object): def foo(self, x): prin ...
- python中@classmethod @staticmethod区别(转)
pthon中3种方式定义类方法, 常规方式, @classmethod修饰方式, @staticmethod修饰方式. class A(object): def foo(self, x): print ...
- [转]python中@classmethod @staticmethod区别
Python中3种方式定义类方法, 常规方式, @classmethod修饰方式, @staticmethod修饰方式. class A(object): def foo(self, x): prin ...
- python的==和is区别
Python中: is判断两个标识符是否引自同一个对象 ==判断两个标识符的值是否相等 区别于java: ==判断两个标识符是否引自同一个对象 .equals()判断是否相等 #如果是String ...
- PyQt的QString和python的string的区别
转载于http://blog.chinaunix.net/uid-200142-id-4018863.html python的string和PyQt的QString的区别 python string和 ...
- js中正则表达式与Python中正则表达式的区别
今天女票让我帮她写一个js中的正则,来提取电话号码,对于正则规则来说,js与python是基本没有区别的,重点的区别是在一些函数与方法中. python中的正则提取: import re str = ...
- 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 > ...
随机推荐
- Java Set List Map
对JAVA的集合的理解是想对于数组 数组是大小固定的,并且同一个数组只能存放类型一样的数据(基本类型/引用类型) JAVA集合可以存储和操作数目不固定的一组数据. 所有的JAVA集合都位于 java. ...
- Socket心跳包机制
心跳包的发送,通常有两种技术方法1:应用层自己实现的心跳包 由应用程序自己发送心跳包来检测连接是否正常,大致的方法是:服务器在一个 Timer事件中定时 向客户端发送一个短小精悍的数据包,然后启动一个 ...
- Linux操作_常用命令操作练习
1,新键一个用户,该用户名为自己姓名首字母缩写+学号最后2位组成(如王东,学号最后2位为18,则该用户名为wd18),为该用户设置密码,并将其加到users组:将该用户的相关信息更改(要求:Name为 ...
- 域名映射ip
windows: 修改文件hosts文件 地址是C:\WINDOWS\system32\drivers\etc\hosts 加进你自己的如: Linux: hosts 文件目录: sudo vi /e ...
- e816. 创建工具栏
A toolbar can be either horizontal or vertical. When the orientation is horizontal, the objects in t ...
- Unity----Scene加载问题
Unity官方提供了4种加载场景(scene)的方法,分别是: 1. Application.LoadLevel():同步加载 2. Application.LoadLevelAsync():异步加载 ...
- 嵌入式开发之hi3516---GV7601 SPI通信问题
http://blog.csdn.net/qq_29350001/article/details/52669964 http://blog.csdn.net/zqj6893/article/detai ...
- C# 指定ip段生成ip地址
private void button1_Click(object sender, EventArgs e) { string StartIp = ""; string EndIp ...
- Linux服务器 java生成的图片验证码乱码问题
问题:如图所示项目中生成的图形验证码不能正常显示出需要的字体 原因: linux下没有对应的字体 查找项目中使用到系统字体的地方,如下: 解决: 1. 在本地 路径 C:\Windows\Fonts ...
- UITableView:改变 TableHeaderView 的高度
参考:http://stackoverflow.com/a/526825 有这么一种需求,在列表顶端显示一些别样的数据,而这个别样的数据则需要通过一个别样的 View 来展现,它便是 UITableV ...