python魔法方法: 增量运算

class A:
  a = 0
  
  def __iadd__(self, b):
    print('->', b)
    self.a += b
    
a = A()
print(a)
print(a.a)

b = a
print(b)
print(b.a)

a += 2
print(a)
print(b)
print(b.a)

在这里插入图片描述

增量运算(+=)时 将调用本身的__iadd__魔法方法,实际上下次使用该变量是是直接使用的增量运算函数的返回值,因此此处a的增量运算返回值为None,而b的结果为2

当我们回归其本质:x += 1 ==> x = x + 1 可以看出,x 其实进行了重新赋值,重新赋值成了 iadd 的返回值。而我们代码示例中,这个方法的返回值是一个字符串。在一开始时,x是我们类的实例。但是在进行了增量运算后,x 变成了魔法方法的返回值,也就是字符串了,所以才会出现以上的报错。

所以我们在使用的时候要注意 x 身份的改变,不然会有许多意想不到的麻烦

还有一些列子:

class Foo(object):
    def __init__(self, x):
        self.x = x

    def __iadd__(self, other):
        return 'Foo iadd: %s + %s' % (self.x, other)

a = Foo(123)
a += 1
print(a)

输出为 Foo iadd: 123 + 1

但是,如果两个对象的实现了__iadd__,情况就会大为不同:

class Foo(object):
    def __init__(self, x):
        self.x = x

    def __iadd__(self, other):
        return 'Foo iadd: %s + %s' % (self.x, other.x)

class Boo(object):
    def __init__(self, x):
        self.x = x

    def __iadd__(self, other):
        return 'Boo iadd: %s + %s' % (self.x, other.x)


a = Foo(123)
b = Boo(321)
a += b
print (a)

输出为 Foo iadd: 123 + 321

看似很正常,然而代码如下时:

a = Foo(123)
b = Boo(321)
a += b
print a
b += a
print b

在这里插入图片描述
报错显示:str没有x这个属性,但是按照代码来看,两个对象都有x属性呀。

在b += a 这行有错,也就是说self为 b,other为 a。后来试验了一番,发现将:

return ‘Boo iadd: %s + %s’ % (self.x, other.x)

改为:

return ‘Boo iadd: %s + %s’ % (self.x, other)

代码就不会报错了,但是输出几个如下:
  在这里插入图片描述
参考:https://www.cnblogs.com/scolia/p/5686267.html

原文地址:https://www.cnblogs.com/kuronekonano/p/11794300.html