Python二元算术运算常用方法解析


Posted in Python onSeptember 15, 2020

在本文中,我想谈谈二元算术运算。具体来说,我想解读减法的工作原理:a - b。我故意选择了减法,因为它是不可交换的。这可以强调出操作顺序的重要性,与加法操作相比,你可能会在实现时误将 a 和 b 翻转,但还是得到相同的结果。

查看 C 代码

按照惯例,我们从查看 CPython 解释器编译的字节码开始。

>>> def sub(): a - b 
... 
>>> import dis 
>>> dis.dis(sub) 
 1      0 LOAD_GLOBAL       0 (a) 
       2 LOAD_GLOBAL       1 (b) 
       4 BINARY_SUBTRACT 
       6 POP_TOP 
       8 LOAD_CONST        0 (None) 
       10 RETURN_VALUE

看起来我们需要深入研究 BINARY_SUBTRACT 操作码。翻查 Python/ceval.c 文件,可以看到实现该操作码的 C 代码如下:

case TARGET(BINARY_SUBTRACT): { 
  PyObject *right = POP(); 
  PyObject *left = TOP(); 
  PyObject *diff = PyNumber_Subtract(left, right); 
  Py_DECREF(right); 
  Py_DECREF(left); 
  SET_TOP(diff); 
  if (diff == NULL) 
  goto error; 
  DISPATCH(); 
}

来源:https://github.com/python/cpython/blob/6f8c8320e9eac9bc7a7f653b43506e75916ce8e8/Python/ceval.c#L1569-L1579

这里的关键代码是PyNumber_Subtract(),实现了减法的实际语义。继续查看该函数的一些宏,可以找到binary_op1() 函数。它提供了一种管理二元操作的通用方法。

不过,我们不把它作为实现的参考,而是要用Python的数据模型,官方文档很好,清楚介绍了减法所使用的语义。

从数据模型中学习

通读数据模型的文档,你会发现在实现减法时,有两个方法起到了关键作用:__sub__ 和 __rsub__。

1、__sub__()方法

当执行a - b 时,会在 a 的类型中查找__sub__(),然后把 b 作为它的参数。这很像我写属性访问的文章 里的__getattribute__(),特殊/魔术方法是根据对象的类型来解析的,并不是出于性能目的而解析对象本身;在下面的示例代码中,我使用_mro_getattr() 表示此过程。

因此,如果已定义 __sub__(),则 type(a).__sub__(a,b) 会被用来作减法操作。(译注:魔术方法属于对象的类型,不属于对象)

这意味着在本质上,减法只是一个方法调用!你也可以将它理解成标准库中的 operator.sub() 函数。

我们将仿造该函数实现自己的模型,用 lhs 和 rhs 两个名称,分别表示 a-b 的左侧和右侧,以使示例代码更易于理解。

# 通过调用__sub__()实现减法 
def sub(lhs: Any, rhs: Any, /) -> Any: 
  """Implement the binary operation `a - b`.""" 
  lhs_type = type(lhs) 
  try: 
    subtract = _mro_getattr(lhs_type, "__sub__") 
  except AttributeError: 
    msg = f"unsupported operand type(s) for -: {lhs_type!r} and {type(rhs)!r}" 
    raise TypeError(msg) 
  else: 
    return subtract(lhs, rhs)

2、让右侧使用__rsub__()

但是,如果 a 没有实现__sub__() 怎么办?如果 a 和 b 是不同的类型,那么我们会尝试调用 b 的 __rsub__()(__rsub__ 里面的“r”表示“右”,代表在操作符的右侧)。

当操作的双方是不同类型时,这样可以确保它们都有机会尝试使表达式生效。当它们相同时,我们假设__sub__() 就能够处理好。但是,即使两边的实现相同,你仍然要调用__rsub__(),以防其中一个对象是其它的(子)类。

3、不关心类型

现在,表达式双方都可以参与运算!但是,如果由于某种原因,某个对象的类型不支持减法怎么办(例如不支持 4 - “stuff”)?在这种情况下,__sub__ 或__rsub__ 能做的就是返回 NotImplemented。

这是给 Python 返回的信号,它应该继续执行下一个操作,尝试使代码正常运行。对于我们的代码,这意味着需要先检查方法的返回值,然后才能假定它起作用。

# 减法的实现,其中表达式的左侧和右侧均可参与运算 
_MISSING = object() 
 
def sub(lhs: Any, rhs: Any, /) -> Any: 
    # lhs.__sub__ 
    lhs_type = type(lhs) 
    try: 
      lhs_method = debuiltins._mro_getattr(lhs_type, "__sub__") 
    except AttributeError: 
      lhs_method = _MISSING 
 
    # lhs.__rsub__ (for knowing if rhs.__rub__ should be called first) 
    try: 
      lhs_rmethod = debuiltins._mro_getattr(lhs_type, "__rsub__") 
    except AttributeError: 
      lhs_rmethod = _MISSING 
 
    # rhs.__rsub__ 
    rhs_type = type(rhs) 
    try: 
      rhs_method = debuiltins._mro_getattr(rhs_type, "__rsub__") 
    except AttributeError: 
      rhs_method = _MISSING 
 
    call_lhs = lhs, lhs_method, rhs 
    call_rhs = rhs, rhs_method, lhs 
 
    if lhs_type is not rhs_type: 
      calls = call_lhs, call_rhs 
    else: 
      calls = (call_lhs,) 
 
    for first_obj, meth, second_obj in calls: 
      if meth is _MISSING: 
        continue 
      value = meth(first_obj, second_obj) 
      if value is not NotImplemented: 
        return value 
    else: 
      raise TypeError( 
        f"unsupported operand type(s) for -: {lhs_type!r} and {rhs_type!r}" 
      )

4、子类优先于父类

如果你看一下__rsub__() 的文档,就会注意到一条注释。它说如果一个减法表达式的右侧是左侧的子类(真正的子类,同一类的不算),并且两个对象的__rsub__() 方法不同,则在调用__sub__() 之前会先调用__rsub__()。换句话说,如果 b 是 a 的子类,调用的顺序就会被颠倒。

这似乎是一个很奇怪的特例,但它背后是有原因的。当你创建一个子类时,这意味着你要在父类提供的操作上注入新的逻辑。这种逻辑不一定要加给父类,否则父类在对子类操作时,就很容易覆盖子类想要实现的操作。

具体来说,假设有一个名为 Spam 的类,当你执行 Spam() - Spam() 时,得到一个 LessSpam 的实例。接着你又创建了一个 Spam 的子类名为 Bacon,这样,当你用 Spam 去减 Bacon 时,你得到的是 VeggieSpam。

如果没有上述规则,Spam() - Bacon() 将得到 LessSpam,因为 Spam 不知道减掉 Bacon 应该得出 VeggieSpam。

但是,有了上述规则,就会得到预期的结果 VeggieSpam,因为 Bacon.__rsub__() 首先会在表达式中被调用(如果计算的是 Bacon() - Spam(),那么也会得到正确的结果,因为首先会调用 Bacon.__sub__(),因此,规则里才会说两个类的不同的方法需有区别,而不仅仅是一个由 issubclass() 判断出的子类。)

# Python中减法的完整实现 
_MISSING = object() 
 
def sub(lhs: Any, rhs: Any, /) -> Any: 
    # lhs.__sub__ 
    lhs_type = type(lhs) 
    try: 
      lhs_method = debuiltins._mro_getattr(lhs_type, "__sub__") 
    except AttributeError: 
      lhs_method = _MISSING 
 
    # lhs.__rsub__ (for knowing if rhs.__rub__ should be called first) 
    try: 
      lhs_rmethod = debuiltins._mro_getattr(lhs_type, "__rsub__") 
    except AttributeError: 
      lhs_rmethod = _MISSING 
 
    # rhs.__rsub__ 
    rhs_type = type(rhs) 
    try: 
      rhs_method = debuiltins._mro_getattr(rhs_type, "__rsub__") 
    except AttributeError: 
      rhs_method = _MISSING 
 
    call_lhs = lhs, lhs_method, rhs 
    call_rhs = rhs, rhs_method, lhs 
 
    if ( 
      rhs_type is not _MISSING # Do we care? 
      and rhs_type is not lhs_type # Could RHS be a subclass? 
      and issubclass(rhs_type, lhs_type) # RHS is a subclass! 
      and lhs_rmethod is not rhs_method # Is __r*__ actually different? 
    ): 
      calls = call_rhs, call_lhs 
    elif lhs_type is not rhs_type: 
      calls = call_lhs, call_rhs 
    else: 
      calls = (call_lhs,) 
 
    for first_obj, meth, second_obj in calls: 
      if meth is _MISSING: 
        continue 
      value = meth(first_obj, second_obj) 
      if value is not NotImplemented: 
        return value 
    else: 
      raise TypeError( 
        f"unsupported operand type(s) for -: {lhs_type!r} and {rhs_type!r}" 
      )

推广到其它二元运算

解决掉了减法运算,那么其它二元运算又如何呢?好吧,事实证明它们的操作相同,只是碰巧使用了不同的特殊/魔术方法名称。

所以,如果我们可以推广这种方法,那么我们就可以实现 13 种操作的语义:+ 、-、*、@、/、//、%、**、<<、>>、&、^、和 |。

由于闭包和 Python 在对象自省上的灵活性,我们可以提炼出 operator 函数的创建。

# 一个创建闭包的函数,实现了二元运算的逻辑 
_MISSING = object() 
 
 
def _create_binary_op(name: str, operator: str) -> Any: 
  """Create a binary operation function. 
 
  The `name` parameter specifies the name of the special method used for the 
  binary operation (e.g. `sub` for `__sub__`). The `operator` name is the 
  token representing the binary operation (e.g. `-` for subtraction). 
 
  """ 
 
  lhs_method_name = f"__{name}__" 
 
  def binary_op(lhs: Any, rhs: Any, /) -> Any: 
    """A closure implementing a binary operation in Python.""" 
    rhs_method_name = f"__r{name}__" 
 
    # lhs.__*__ 
    lhs_type = type(lhs) 
    try: 
      lhs_method = debuiltins._mro_getattr(lhs_type, lhs_method_name) 
    except AttributeError: 
      lhs_method = _MISSING 
 
    # lhs.__r*__ (for knowing if rhs.__r*__ should be called first) 
    try: 
      lhs_rmethod = debuiltins._mro_getattr(lhs_type, rhs_method_name) 
    except AttributeError: 
      lhs_rmethod = _MISSING 
 
    # rhs.__r*__ 
    rhs_type = type(rhs) 
    try: 
      rhs_method = debuiltins._mro_getattr(rhs_type, rhs_method_name) 
    except AttributeError: 
      rhs_method = _MISSING 
 
    call_lhs = lhs, lhs_method, rhs 
    call_rhs = rhs, rhs_method, lhs 
 
    if ( 
      rhs_type is not _MISSING # Do we care? 
      and rhs_type is not lhs_type # Could RHS be a subclass? 
      and issubclass(rhs_type, lhs_type) # RHS is a subclass! 
      and lhs_rmethod is not rhs_method # Is __r*__ actually different? 
    ): 
      calls = call_rhs, call_lhs 
    elif lhs_type is not rhs_type: 
      calls = call_lhs, call_rhs 
    else: 
      calls = (call_lhs,) 
 
    for first_obj, meth, second_obj in calls: 
      if meth is _MISSING: 
        continue 
      value = meth(first_obj, second_obj) 
      if value is not NotImplemented: 
        return value 
    else: 
      exc = TypeError( 
        f"unsupported operand type(s) for {operator}: {lhs_type!r} and {rhs_type!r}" 
      ) 
      exc._binary_op = operator 
      raise exc

有了这段代码,你可以将减法运算定义为 _create_binary_op(“sub”, “-”),然后根据需要重复定义出其它运算。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
详解Python中的循环语句的用法
Apr 09 Python
Python中文件操作简明介绍
Apr 13 Python
详解 Python中LEGB和闭包及装饰器
Aug 03 Python
Python读取mat文件,并转为csv文件的实例
Jul 04 Python
Python格式化输出字符串方法小结【%与format】
Oct 29 Python
浅谈Python中的可迭代对象、迭代器、For循环工作机制、生成器
Mar 11 Python
Python 串口读写的实现方法
Jun 12 Python
python选取特定列 pandas iloc,loc,icol的使用详解(列切片及行切片)
Aug 06 Python
python 字典 setdefault()和get()方法比较详解
Aug 07 Python
python Kmeans算法原理深入解析
Aug 23 Python
Python使用tkinter实现小时钟效果
Feb 22 Python
opencv深入浅出了解机器学习和深度学习
Mar 17 Python
Python实现像awk一样分割字符串
Sep 15 #Python
详解Pycharm安装及Django安装配置指南
Sep 15 #Python
Java Unsafe类实现原理及测试代码
Sep 15 #Python
python装饰器实现对异常代码出现进行自动监控的实现方法
Sep 15 #Python
Python requests上传文件实现步骤
Sep 15 #Python
python -v 报错问题的解决方法
Sep 15 #Python
基于Python正确读取资源文件
Sep 14 #Python
You might like
php 从数据库提取二进制图片的处理代码
2009/09/09 PHP
PHP基于imap获取邮件实例
2014/11/11 PHP
php去除二维数组的重复项方法
2015/11/03 PHP
WordPress中获取所使用的模板的页面ID的简单方法
2015/12/31 PHP
laravel学习教程之关联模型
2016/07/30 PHP
基于ThinkPHP实现的日历功能实例详解
2017/04/15 PHP
js 动态选中下拉框
2009/11/26 Javascript
IE8下关于querySelectorAll()的问题
2010/05/13 Javascript
关于捕获用户何时点击window.onbeforeunload的取消事件
2011/03/06 Javascript
一个简单的JS时间控件示例代码(JS时分秒时间控件)
2013/11/22 Javascript
jQuery对象和DOM对象之间相互转换的方法介绍
2015/02/28 Javascript
jQuery实现点击按钮弹出可关闭层的浮动层插件
2015/09/19 Javascript
详解JavaScript的流程控制语句
2015/11/30 Javascript
简单实现JS对dom操作封装
2015/12/02 Javascript
利用forever和pm2部署node.js项目过程
2017/05/10 Javascript
对存在JavaScript隐式类型转换的四种情况的总结(必看篇)
2017/08/31 Javascript
利用纯js + transition动画实现移动端web轮播图详解
2017/09/10 Javascript
JavaScript基础之静态方法和实例方法分析
2018/12/26 Javascript
babel7.x和webpack4.x配置vue项目的方法步骤
2019/05/12 Javascript
layui 对弹窗 form表单赋值的实现方法
2019/09/04 Javascript
微信小程序制作扭蛋机代码实例
2019/09/24 Javascript
jquery实现吸顶导航效果
2020/01/08 jQuery
jquery实现广告上下滚动效果
2021/03/04 jQuery
python判断windows系统是32位还是64位的方法
2015/05/11 Python
Python中super()函数简介及用法分享
2016/07/11 Python
django实现登录时候输入密码错误5次锁定用户十分钟
2017/11/05 Python
python写入已存在的excel数据实例
2018/05/03 Python
pyqt5让图片自适应QLabel大小上以及移除已显示的图片方法
2019/06/21 Python
python 实现绘制整齐的表格
2019/11/18 Python
学习Python爬虫的几点建议
2020/08/05 Python
详解Python3 定义一个跨越多行的字符串的多种方法
2020/09/06 Python
button在IE6/7下的黑边去除方案
2012/12/24 HTML / CSS
Clarks其乐鞋荷兰官网:Clarks荷兰
2019/07/05 全球购物
民政局标准版离婚协议书
2014/12/01 职场文书
运动会800米赞词
2015/07/22 职场文书
MySQL批量更新不同表中的数据
2022/05/11 MySQL