Python编程中内置的NotImplemented类型的用法


Posted in Python onMarch 23, 2022

一、NotImplemented它是什么?

>>> type(NotImplemented)
<type 'NotImplementedType'>

NotImplementedPython在内置命名空间中的六个常数之一。其他有FalseTrueNoneEllipsisdebug。和 Ellipsis很像,[NotImplemented] 能被重新赋值(覆盖)。对它赋值,甚至改变属性名称, 不会产生 SyntaxError。所以它不是一个真正的“真”常数。当然,我们应该永远不改变它。

但是为了完整性:

>>> None = 'hello'
...
SyntaxError: can't assign to keyword
>>> NotImplemented
NotImplemented
>>> NotImplemented = 'do not'
>>> NotImplemented
'do not'

二、它有什么用?什么时候用?

NotImplemented 是个特殊值,它能被二元特殊方法返回(比如__eq__()、 lt() 、 add() 、 rsub() 等),表明某个类型没有像其他类型那样实现这些操作。同样,它或许会被原地处理(in place)的二元特殊方法返回(比如__imul__()、iand()等)。

还有,它的实际值为True:

>>> bool(NotImplemented)
True

你也许会问自己,“但我认为当这个操作没有实现时,我应该产生个NotImpementedError”。我们会看些例子,关于为什么当实现二元特殊方法时不是这么回事儿。

让我们看看NotImplemented常数的用法,通过__eq__()对于两个非常基本(且没用)的类 A 和 B 的编码。对于这个简单的例子,为了避免干扰,不会实现__ne__() ,但是总的说来,每次实现__eq__() 时, ne()也应该被实现,除非,有个足够充分的理由去不实现它。

# example.py
  
class A(object):
  def __init__(self, value):
    self.value = value
  
  def __eq__(self, other):
    if isinstance(other, A):
      print('Comparing an A with an A')
      return other.value == self.value
    if isinstance(other, B):
      print('Comparing an A with a B')
      return other.value == self.value
    print('Could not compare A with the other class')
    return NotImplemented
  
class B(object):
  def __init__(self, value):
    self.value = value
  
  def __eq__(self, other):
    if isinstance(other, B):
      print('Comparing a B with another B')
      return other.value == self.value
    print('Could not compare B with the other class')
    return NotImplemented

现在,在解释器中:

>>> from example import A, B
>>> a1 = A(1)
>>> b1 = B(1)

我们现在可以实验下对于 eq() 不同的调用,看看发生了什么。

作为提醒,在Python中,a == b会调用a.eq(b):

>>> a1 == a1
Comparing an A with an A
True

正如所望,a1等于a1(自己),使用类A中的__eq__()来进行这个比较的。

比较b1和它自己也会产生类似结果:

>>> b1 == b1
Comparing a B with another B
True

现在,那要是我们比较a1和b1呢?由于在A的__eq__()会检查other是不是B的一个实例,我们想要a1.eq(b1)去处理这个比较并返回True:

>>> a1 == b1
Comparing an A with a B
True

就是这样。现在,如果我们比较b1和a1(即调用b1.eq(a1)),我们会想要返回NotImplemented。这是因为B的__eq__()只和其他B的实例进行比较。

来看看发生了什么:

>>> b1 == a1
Could not compare B against the other class
Comparing an A with a B
True

聪明!b1.eq(a1)方法返回NotImplemented,这样会导致调用A中的__eq__()方法。而且由于在A中的__eq__()定义了A和B之间的比较,所以就得到了正确的结果(True)。

这就是返回了NotImplemented的所做的。NotImplemented告诉运行时,应该让其他对象来完成某个操作。在表达b1 == a1中,b1.eq(a1)返回了NotImplemented,这说明Python试着用a1.eq(b1)。由于a1足够可以返回True,因此这个表达可以成功。如果A中的__eq__()也返回NotImplemented,那么运行时会退化到使用内置的比较行为,即比较对象的标识符(在CPython中,是对象在内存中的地址)。

注意:如果在调用b1.eq(a1)时抛出NotImpementedError,而不进行处理,就会中断代码的执行。而NotImplemented无法抛出,仅仅是用来进一步测试是否有其他方法可供调用。

到此这篇关于Python编程中内置的NotImplemented类型的用法的文章就介绍到这了,更多相关Python内置NotImplemented类型内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python爬取网站数据保存使用的方法
Nov 20 Python
Python Learning 列表的更多操作及示例代码
Aug 22 Python
windows下cx_Freeze生成Python可执行程序的详细步骤
Oct 09 Python
Python 2/3下处理cjk编码的zip文件的方法
Apr 26 Python
如何使用Python实现斐波那契数列
Jul 02 Python
python简单实现矩阵的乘,加,转置和逆运算示例
Jul 10 Python
python遍历文件目录、批量处理同类文件
Aug 31 Python
Python Django模板之模板过滤器与自定义模板过滤器示例
Oct 18 Python
常用的10个Python实用小技巧
Aug 10 Python
python线程池 ThreadPoolExecutor 的用法示例
Oct 10 Python
快速创建python 虚拟环境
Nov 28 Python
python通用数据库操作工具 pydbclib的使用简介
Dec 21 Python
pandas进行数据输入和输出的方法详解
Mar 23 #Python
基于Python编写简易版的天天跑酷游戏的示例代码
Python中的嵌套循环详情
Mar 23 #Python
python装饰器代码解析
Mar 23 #Python
基于Python实现将列表数据生成折线图
Python必备技巧之字符数据操作详解
Pytorch中使用ImageFolder读取数据集时忽略特定文件
Mar 23 #Python
You might like
在 PHP 中使用随机数的三个步骤
2006/10/09 PHP
Yii2中使用join、joinwith多表关联查询
2016/06/30 PHP
golang、python、php、c++、c、java、Nodejs性能对比
2017/03/12 NodeJs
Laravel5.5 手动分页和自定义分页样式的简单实现
2019/10/15 PHP
js对象之JS入门之Array对象操作小结
2011/01/09 Javascript
给页面渲染时间加速 干掉Dom Level 0 Event
2012/12/19 Javascript
使用NodeJs 开发微信公众号(三)微信事件交互实例
2016/03/02 NodeJs
JavaScript获取图片像素颜色并转换为box-shadow显示
2016/03/11 Javascript
javascript模块化简单解析
2016/04/07 Javascript
bootstrap读书笔记之CSS组件(上)
2016/10/17 Javascript
全屏滚动插件fullPage.js使用实例解析
2016/10/21 Javascript
AngularJS压缩JS技巧分析
2016/11/08 Javascript
vue-router 学习快速入门
2017/03/01 Javascript
基于js中style.width与offsetWidth的区别(详解)
2017/11/12 Javascript
vue中使用vue-router切换页面时滚动条自动滚动到顶部的方法
2017/11/28 Javascript
React Router v4 入坑指南(小结)
2018/04/08 Javascript
详解使用jQuery.i18n.properties实现js国际化
2018/05/04 jQuery
vue实现未登录跳转到登录页面的方法
2018/07/17 Javascript
详解Webpack如何引入CDN链接来优化编译后的体积
2019/06/21 Javascript
Python2.x利用commands模块执行Linux shell命令
2016/03/11 Python
Python使用tablib生成excel文件的简单实现方法
2016/03/16 Python
详解django.contirb.auth-认证
2018/07/16 Python
Python统计纯文本文件中英文单词出现个数的方法总结【测试可用】
2018/07/25 Python
用pycharm开发django项目示例代码
2018/10/24 Python
python 文本单词提取和词频统计的实例
2018/12/22 Python
Python面向对象程序设计之类和对象、实例变量、类变量用法分析
2020/03/23 Python
Win10下配置tensorflow-gpu的详细教程(无VS2015/2017)
2020/07/14 Python
国际商务专业毕业生自我鉴定2014
2014/09/27 职场文书
一般基层干部群众路线教育实践活动个人对照检查材料
2014/11/04 职场文书
教师听课评语大全
2014/12/31 职场文书
品质保证书格式
2015/02/28 职场文书
创先争优个人总结
2015/03/04 职场文书
2015暑期社会实践调查报告
2015/07/14 职场文书
教师师德承诺书2016
2016/03/25 职场文书
tensorflow学习笔记之tfrecord文件的生成与读取
2021/03/31 Python
win10识别不了U盘怎么办 win10系统读取U盘失败的解决办法
2022/08/05 数码科技