[Numpy-discussion] Regression: in-place operations (possibly intentional)
Nathaniel Smith
njs@pobox....
Fri Sep 21 12:03:09 CDT 2012
On 21 Sep 2012 17:31, "Chris Barker" <chris.barker@noaa.gov> wrote:
>
> On Thu, Sep 20, 2012 at 2:48 PM, Nathaniel Smith <njs@pobox.com> wrote:
> > because a += b
> > really should be the same as a = a + b.
>
> I don't think that's the case - the inplace operator should be (and
> are) more than syntactic sugar -- they have a different meaning and
> use (in fact, I think they should't work at all for immutable, sbut i
> guess the common increment-a-counter use was too good to pass up)
>
> in the numpy case:
>
> a = a + b
>
> means "make a new array, from the result of adding a and b"
>
> whereas:
>
> a += b
>
> means "change a in place by adding b to it"
>
> In the first case, I'd expect the type of the result to be determined
> by both a and b -- casting rules.
>
> In the second case, a should certainly not be a different object, and
> should not have a new data buffer, therefor should not change type.
You're right of course. What I meant is that
a += b
should produce the same result as
a[...] = a + b
If we change the casting rule for the first one but not the second, though,
then these will produce different results if a is integer and b is float:
the first will produce an error, while the second will succeed, silently
discarding fractional parts.
-n
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20120921/ef6cb933/attachment.html
More information about the NumPy-Discussion
mailing list