Python的增量赋值问题 [英] Python augmented assignment issue

查看:674
本文介绍了Python的增量赋值问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些有趣的关于蟒蛇增量赋值 + =

i ran into something interesting about the python augmented assignment +=

这似乎是自动数据类型转换并不总是做 A + = B 如果是一个'简单'的数据类型,而 A = A + b 似乎总是工作

it seems to be automatic data type conversion is not always done for a += b if a is a 'simpler' data type, while a = a + b seems to work always

在这里转换完成情况

a = 1
b = 1j

a = 1
b = 0.5

情况下转换尚未完成。

case where the conversion is not done

from numpy import array
a = array([0, 0 ,0])
b = array([0, 0, 1j])

A + = B A 仍然是整数矩阵,而不是复杂的矩阵

after a += b, a remains as integer matrix, instead of complex matrix

我曾经以为 A + = B 相同 A = A + B ,请问是什么在底层实现他们的区别呢?

i used to think a += b is the same as a = a + b, what is the difference of them in the underlying implementation?

推荐答案

对于 + 运营商,Python中定义了一个对象可以实现三个特殊的方法:

For the + operator, Python defines three "special" methods that an object may implement:


  • __ __添加:增加了两个项目( + 运营商)。当你做 A + B __加__ 方法 A 被调用 b 作为参数。<​​/ li>
  • __ __ RADD :反映增加;为 A + B __ RADD __ B法是调用 A 作为一个实例。这仅用于在 A 不知道该怎么办的添加和两个对象是不同的类型。

  • __ __ IADD :就地增加;用于 A + = B 其中结果被分配回左边的变量。此分开设置,因为它有可能实现它在一个更有效的方式。例如,如果 A 是一个列表,那么 A + = B 相同 a.extend(二)。然而,在 C = A +的情况B 你必须做出的一个副本 A ,然后扩展它,因为 A 是不是可以在这种情况下修改。需要注意的是,如果你不执行 __ IADD __ 则Python会只是调用 __添加__ 代替。

  • __add__: adds two items (+ operator). When you do a + b, the __add__ method of a is called with b as an argument.
  • __radd__: reflected add; for a + b, the __radd__ method of b is called with a as an instance. This is only used when a doesn't know how to do the add and the two objects are different types.
  • __iadd__: in-place add; used for a += b where the result is assigned back to the left variable. This is provided separately because it might be possible to implement it in a more efficient way. For example, if a is a list, then a += b is the same as a.extend(b). However, in the case of c = a + b you have to make a copy of a before you extend it since a is not to be modified in this case. Note that if you don't implement __iadd__ then Python will just call __add__ instead.

所以,因为这些不同的操作是有独立的方法来实现,这是可能的(但一般不好的做法)来实现他们,让他们做完全不同的事情,或者在这种情况下,只有的不同的事情

So since these different operations are implemented with separate methods, it is possible (but generally bad practice) to implement them so they do totally different things, or perhaps in this case, only slightly different things.

还有人推断,你使用numpy的,并解释其行为。但是,你问的底层实现。希望你现在看到的为什么的,有时的情况是 A + = B 是不一样的 A = A + b 。顺便说一下,一个类似的方法三人也可以用于其它操作来实现。请参见此页面为所有支持就地方法的列表。

Others have deduced that you're using NumPy and explained its behavior. However, you asked about the underlying implementation. Hopefully you now see why it is sometimes the case that a += b is not the same as a = a + b. By the way, a similar trio of methods may also be implemented for other operations. See this page for a list of all the supported in-place methods.

这篇关于Python的增量赋值问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆