[issue18305] [patch] Fast sum() for non-numbers
Oscar Benjamin
report at bugs.python.org
Thu Jul 11 16:39:23 CEST 2013
Oscar Benjamin added the comment:
This "optimisation" is a semantic change. It breaks backward compatibility in cases where a = a + b and a += b do not result in the name a having the same value. In particular this breaks backward compatibility for numpy users.
Numpy arrays treat += differently from + in the sense that a += b coerces b to the same dtype as a and then adds in place whereas a + b uses Python style type promotion. This behaviour is by design and it is useful. It is also entirely appropriate (unlike e.g. summing lists) that someone would use sum() to add numpy arrays.
An example where + and += give different results:
>>> from numpy import array
>>> a1 = array([1, 2, 3], dtype=int)
>>> a1
array([1, 2, 3])
>>> a2 = array([.5, .5, .5], dtype=float)
>>> a2
array([ 0.5, 0.5, 0.5])
>>> a1 + a2
array([ 1.5, 2.5, 3.5])
>>> a1 += a2
>>> a1
array([1, 2, 3])
----------
nosy: +oscarbenjamin
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue18305>
_______________________________________
More information about the Python-bugs-list
mailing list