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多线程学习资料
Dec 19 Python
Python中在for循环中嵌套使用if和else语句的技巧
Jun 20 Python
python format 格式化输出方法
Jul 16 Python
Python网页正文转换语音文件的操作方法
Dec 09 Python
Python-Tkinter Text输入内容在界面显示的实例
Jul 12 Python
Flask框架单例模式实现方法详解
Jul 31 Python
pygame实现俄罗斯方块游戏(对战篇1)
Oct 29 Python
python 申请内存空间,用于创建多维数组的实例
Dec 02 Python
Selenium 滚动页面至元素可见的方法
Mar 18 Python
使用matlab 判断两个矩阵是否相等的实例
May 11 Python
Python实现Word文档转换Markdown的示例
Dec 22 Python
PySwarms(Python粒子群优化工具包)的使用:GlobalBestPSO例子解析
Apr 05 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动态创建Flash动画
2006/10/09 PHP
PHP HTML代码串截取代码
2008/12/29 PHP
Cygwin中安装PHP方法步骤
2015/07/04 PHP
PHP实现获取某个月份周次信息的方法
2015/08/11 PHP
php连接oracle数据库的核心步骤
2016/05/26 PHP
JavaScript自执行闭包的小例子
2013/06/29 Javascript
jquery让返回的内容显示在特定div里(代码少而精悍)
2014/06/23 Javascript
jQuery实现列表自动滚动循环滚动展示新闻
2014/08/22 Javascript
js查找节点的方法小结
2015/01/13 Javascript
jquery事件的ready()方法使用详解
2015/11/11 Javascript
轻松掌握JavaScript装饰者模式
2016/08/27 Javascript
JavaScript 冒泡排序和选择排序的实现代码
2016/09/03 Javascript
jQuery+ajax读取并解析XML文件的方法
2016/09/09 Javascript
jQuery动态创建元素以及追加节点的实现方法
2016/10/20 Javascript
vue单页面打包文件大?首次加载慢?nginx带你飞,从7.5M到1.3M蜕变过程(推荐)
2018/01/16 Javascript
实例分析编写vue组件方法
2019/02/12 Javascript
js+html实现周岁年龄计算器
2019/06/25 Javascript
jquery多级树形下拉菜单的实例代码
2019/07/09 jQuery
Vue根据条件添加click事件的方式
2019/11/09 Javascript
vue项目中使用vue-layer弹框插件的方法
2020/03/11 Javascript
JavaScript实现网页跨年倒计时
2020/12/02 Javascript
用python实现简单EXCEL数据统计的实例
2017/01/24 Python
python安装oracle扩展及数据库连接方法
2017/02/21 Python
利用Celery实现Django博客PV统计功能详解
2017/05/08 Python
python如何定义带参数的装饰器
2018/03/20 Python
Python Cookie 读取和保存方法
2018/12/28 Python
python3实现钉钉消息推送的方法示例
2019/03/14 Python
Python 使用folium绘制leaflet地图的实现方法
2019/07/05 Python
在pycharm中创建django项目的示例代码
2020/05/28 Python
Python3.9 beta2版本发布了,看看这7个新的PEP都是什么
2020/06/10 Python
Python实例方法、类方法、静态方法区别详解
2020/09/05 Python
python爬虫多次请求超时的几种重试方法(6种)
2020/12/01 Python
市场部管理制度
2014/02/02 职场文书
党的群众路线教育实践活动自我剖析材料
2014/10/08 职场文书
2019大学毕业晚会主持词
2019/06/21 职场文书
Spring Cloud OpenFeign模版化客户端
2022/06/25 Java/Android