[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 > ...
随机推荐
- 如何将Mac系统OS X Yosemite装到外部磁盘?(转)
有Mac用户想了解是否可以将 OS X Yosemite 装到替代的外部磁盘,答案是肯定的,有需求的用户,可以按照本篇教程逐步进行. 前面我已经写过一篇文章介绍了如何安装 OS X Yosemite ...
- Android Studio错误提示:Gradle project sync failed. Basic functionality (eg. editing, debugging) will not work properly
Android Studio中出现提示: Gradle project sync failed. Basic functionality (eg. editing, debugging) will n ...
- 自然语言交流系统 phxnet团队 创新实训 个人博客 (十三)
关于java socket的一些总结: Java Socket通信有很多的时候需要我们不断的学习.方面效率虽然不及C与C++但它以灵活语言优势,为大家广为使用. 本文就对在使用java做通信方面程序时 ...
- python 查看目录下所有目录和文件
python查看目录下所有的子目录和子文件 python递归遍历目录结构 我喜欢第一种 方法1 import json, os def list_dir(path, res): for i in os ...
- App Store那些事儿
5条建议优化手机游戏的苹果App Store截图 第一:遵守应用商店的规定 进入应用商店的每一款应用都要加上至少一张截图,并且尺寸大小必须符合应用商店的惯例. 第二:显示营销信息 既然你已经知道规定的 ...
- (笔记)Linux下检测网卡与网线连接状态
http://blog.chinaunix.net/space.php?uid=20357359&do=blog&cuid=1798479 Linux下检测网卡与网线连接状态,使用io ...
- Java数组排序和插入
如何排序数组并插入元素? 以下示例显示如何使用sort()方法和用户定义的insertElement()方法来完成此任务. package com.yiibai; import java.util.* ...
- php后台管理员权限相关表结构
admin管理员表 id int(11) 用户id username varchar(128) 用户名 password varchar(128) 管理员密码 name varchar(50) 管理员 ...
- MATLAB:保存mat文件
X_Y_pred_00_0000.mat里存有三个mat文件: 另存:
- 内存管理 初始化(六)vmalloc_init 及 ioremap
是不是我错了,本想这个函数会如网上所说将进行非连续内存管理的初始化,但是对于2.6.34的ARM架构而言,该函数实际完成的业务非常少. 内存管理的初始化读到此处,我感觉原有的认识存在很大缺陷: (1) ...