在这种情况下,为什么x += y
产生与x = x + y
不同的结果?Numpy,为什么`x + = y`产生与`x = x + y`不同的结果?
import numpy as np
x = np.repeat([1], 10)
y = np.random.random(len(x))
x += y
print x
# Output: [1 1 1 1 1 1 1 1 1 1]
x = x + y
print x
# Output: [ 1.50859536 1.31434732 1.15147365 1.76979431 1.64727364
# 1.02372535 1.39335253 1.71878847 1.48823703 1.99458116]
确实是一个重复的。简短的回答:取决于执行魔法“dunder”方法'__add__'和'__iadd__' –
**这不是重复的**,这是特定于'numpy' – jamylak
@jamylak哦 - 的确,谢谢! –