[Python] NotImplemented 和 NotImplementedError 区别
来源:互联网 发布:php导入excel到数据库 编辑:程序博客网 时间:2024/06/07 12:21
NotImplemented 是一个非异常对象,NotImplementedError 是一个异常对象。
>>> NotImplementedNotImplemented>>> NotImplementedError<type 'exceptions.NotImplementedError'>>>> type(NotImplemented)<type 'NotImplementedType'>>>> type(NotImplementedError)<type 'type'>
如果抛出 NotImplemented 会得到 TypeError,因为它不是一个异常。而抛出 NotImplementedError 会正常捕获该异常。
>>> raise NotImplementedTraceback (most recent call last): File "<pyshell#10>", line 1, in <module> raise NotImplementedTypeError: exceptions must be old-style classes or derived from BaseException, not NotImplementedType>>> raise NotImplementedErrorTraceback (most recent call last): File "<pyshell#11>", line 1, in <module> raise NotImplementedErrorNotImplementedError
为什么要存在一个 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 会继续尝试其他方法,例如使用 != 来比较。
ref:http://www.cnblogs.com/ifantastic/p/3682268.html
0 0
- [Python] NotImplemented 和 NotImplementedError 区别
- Python运行错误,raise notImplementedError
- Python编程中NotImplementedError的使用
- Python之美[从菜鸟到高手]--NotImplemented小析
- Python之美[从菜鸟到高手]--NotImplemented小析
- Python之美[从菜鸟到高手]--NotImplemented小析
- Python之美[从菜鸟到高手]--NotImplemented小析
- python cookielib.LWPCookieJar Python中使用cookielib的FileCookieJar去save(),结果出错:NotImplementedError
- 【已解决】Python中使用cookielib的FileCookieJar去save(),结果出错:NotImplementedError
- python读取.mat文件时出现’NotImplementedError: Please use HDF reader for matlab v7.3 files‘问题的解决方案
- python读取.mat文件时出现’NotImplementedError: Please use HDF reader for matlab v7.3 files‘问题的解决方案
- Python __call__ 和 __init__ 区别
- python range和xrange区别
- easy_install和pip区别 python
- python input 和raw_input 区别
- python和Java的区别
- Python @classmethod和@staticmethod区别
- Python 3.5 和 2.7区别
- 无刷新方式上传文件
- Android Binder
- Android UI体验之全屏沉浸式透明状态栏效果
- 【使用Common-FileUpload框架上传文件基本代码】
- ROVIO detailed installation steps from scrach
- [Python] NotImplemented 和 NotImplementedError 区别
- vector常用算法
- JQuery实现回车代替Tab键
- Spring MVC 学习笔记 json格式的输入和输出
- 显示Gralloc模块分配内存(buffer_handle_t、private_handle_t数据结构的关系)
- 实现键盘记录的e.Whick和keyCode
- DOM对象与Jquery对象的区分
- python的本地变量,全局变量,函数的作用域,嵌套函数引用包装函数
- 设计模式学习笔记——策略(Strategy)模式