【发布时间】:2013-06-21 06:24:30
【问题描述】:
在这种情况下,为什么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 哦 - 确实,很好,谢谢!
标签: python python-2.7 numpy