(错点)魔方方法__add__与__radd__

如果右操作数的类型是左操作数类型的子类,并且该子类提供了操作的反射方法,则该方法将在左操作数的非反射方法之前被调用。

If the right operand’s type is a subclass of the left operand’s type and that subclass provides the reflected method for the operation, this method will be called before the left operand’s non-reflected method. This behavior allows subclasses to override their ancestors’ operations.


这样就解释了左右操作的不同,之前自己老是想着自定义的优先

>>> class Nint(int):

        def __radd__(self,other):

                return int.__sub__(other,self)

>>> a=Nint(5)

>>> b=Nint(3)

>>> 1+b

-2

>>> b+1

4

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,505评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,150评论 0 23
  • 走在大桥上,看见一妹子要自杀,我说,都要死了,让我爽一下。没想到她居然答应了。于是我走了过去,一脚把她踹了下去,艹...
    山的那一边_ChenLei阅读 334评论 0 0
  • 不停向东 我打马走过夜的长街 鞍上的思绪 一如身后飞溅的马蹄 踏过森山丛林 如同禅悦引领着我的灵魂 乘着寂寥无限穿...
    爱上一叶浮萍阅读 273评论 6 19
  • 曾经的作品,曾经昵称!来一个过渡篇吧 ①过去:雨霖铃 封面☞
    白云灵阅读 114评论 0 1