在"for"循环中,i = i + 1和i + = 1有什么区别? [英] What is the difference between i = i + 1 and i += 1 in a 'for' loop?

查看:346
本文介绍了在"for"循环中,i = i + 1和i + = 1有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天发现了一件奇怪的事情,想知道是否有人可以弄清楚这里的区别是什么?

I found out a curious thing today and was wondering if somebody could shed some light into what the difference is here?

import numpy as np

A = np.arange(12).reshape(4,3)
for a in A:
    a = a + 1

B = np.arange(12).reshape(4,3)
for b in B:
    b += 1

运行每个for循环后,A不变,但是B已向每个元素添加一个.我实际上使用B版本在for循环内写入初始化的NumPy数组.

After running each for loop, A has not changed, but B has had one added to each element. I actually use the B version to write to a initialized NumPy array within a for loop.

推荐答案

区别在于,一个修改数据结构本身(就地操作)b += 1,而另一个修改重新分配,变量a = a + 1.

The difference is that one modifies the data-structure itself (in-place operation) b += 1 while the other just reassigns the variable a = a + 1.

只是为了完整性:

x += y 并不总是进行就地操作,(至少)三个例外:

x += y is not always doing an in-place operation, there are (at least) three exceptions:

  • 如果x 没有实现一个__iadd__方法,则x += y语句只是x = x + y的简写.如果x类似于int.

  • If x doesn't implement an __iadd__ method then the x += y statement is just a shorthand for x = x + y. This would be the case if x was something like an int.

如果__iadd__返回NotImplemented,Python会退回到x = x + y.

If __iadd__ returns NotImplemented, Python falls back to x = x + y.

__iadd__方法在理论上可以实现为无法正常运行.不过,这样做真的很奇怪.

The __iadd__ method could theoretically be implemented to not work in place. It'd be really weird to do that, though.

实际上,您的b是实现__iadd__numpy.ndarray并返回自身,因此您的第二个循环就地修改了原始数组.

As it happens your bs are numpy.ndarrays which implements __iadd__ and return itself so your second loop modifies the original array in-place.

您可以在模拟数值类型"的Python文档中阅读有关此内容的更多信息.

调用这些[__i*__]方法以实现增强的算术分配(+=-=*=@=/=//=%=**=<<=>>=&=^=|=).这些方法应尝试就地执行操作(修改self)并返回结果(可以是,但不一定是self).如果未定义特定方法,则扩展分配将回退到常规方法.例如,如果x是具有__iadd__()方法的类的实例,则x += y等效于x = x.__iadd__(y).否则,与x + y的评估一样,将考虑x.__add__(y)y.__radd__(x).在某些情况下,扩充分配可能会导致意外错误(请参见为什么a_tuple[i] += ["item"]加法有效时会引发异常吗?),但实际上,这种行为是数据模型的一部分.

These [__i*__] methods are called to implement the augmented arithmetic assignments (+=, -=, *=, @=, /=, //=, %=, **=, <<=, >>=, &=, ^=, |=). These methods should attempt to do the operation in-place (modifying self) and return the result (which could be, but does not have to be, self). If a specific method is not defined, the augmented assignment falls back to the normal methods. For instance, if x is an instance of a class with an __iadd__() method, x += y is equivalent to x = x.__iadd__(y) . Otherwise, x.__add__(y) and y.__radd__(x) are considered, as with the evaluation of x + y. In certain situations, augmented assignment can result in unexpected errors (see Why does a_tuple[i] += ["item"] raise an exception when the addition works?), but this behavior is in fact part of the data model.

这篇关于在"for"循环中,i = i + 1和i + = 1有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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