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 会继续尝试其他方法,例如使用 != 来比较。  

参考一

最新文章

  1. hibernate5.2需要的最少jar文件
  2. Scrum Meeting
  3. java变量
  4. Android UI性能优化实战, 识别View中的性能问题
  5. 如何把一个android工程作为另外一个android工程的lib库
  6. GITHUB使用简介
  7. 【转】安装Django
  8. android的jni
  9. const用法
  10. mvc请求过程总结
  11. IntelliJ IDEA 2018 设置代码超出限制自动换行(最新版)
  12. 【ASP.NET MVC系列】浅谈ASP.NET 程序发布过程
  13. linux df
  14. 解决eclipse新建项目看不到src/main/java目录办法
  15. easymock单元测试跟踪工具
  16. LightOj 1118 - Incredible Molecules(两圆的交集面积)
  17. 【转】每天一个linux命令(53):route命令
  18. ubuntu workbench
  19. Ubuntu 如何更换阿里源
  20. 百度地图隐藏LOGO显示

热门文章

  1. WPF ICommandSource Implementations Leak Memory!
  2. Apache Storm内部原理分析
  3. Intel edison 智能硬件开发指南 基于YoctoProject
  4. Linux命令_用户和用户组管理
  5. 第三百八十八节,Django+Xadmin打造上线标准的在线教育平台—网站列表分页
  6. MySQL中information_schema是什么
  7. JS 在火狐浏览器下关闭弹窗
  8. 指定webapi 返回 json 格式 ; GlobalConfiguration.Configuration.Formatters.Clear()
  9. 逻辑卷管理LVM 扩容LV容量实例(一)
  10. asp.net mvc用aspose.cells 导出xlsx格式的excel。无残留