[Numpy-discussion] augmented assignment and in-place operations
Wed Oct 29 23:18:26 CDT 2008
On Wed, Oct 29, 2008 at 22:37, Alan G Isaac <firstname.lastname@example.org> wrote:
> On 10/29/2008 3:43 PM Robert Kern wrote:
>> The defining characteristic is
>> that "x <op>= y" should be equivalent to "x = x <op> y" except
>> possibly for *optional* in-place semantics.
> This gets at a bit of the Language Reference that I've
> never understood.
> when possible, the actual operation is performed
> in-place, meaning that rather than creating a new
> object and assigning that to the target, the old
> object is modified instead.
> What does that mean? I assume "when possible" means in part
> "when mutable", ruling out e.g. Python integers or floats.
In part, yes. But also "when possible" excludes in-place matrix
multiplication on ndarrays since we don't, as numpy policy, resize
> But the rest does not really seem part of the language, but
> rather seems to be normative? That is, I could define
> __iadd__ anyway I want. Is the above saying that I "should"
> define __iadd__ to do an in-place operation "if possible".
> If so, why is such a normative statement part of the
> language reference? Or is it a statement about the language
> that I'm just not getting?
It is a guide to the intended usage of the feature. You can see other
such guides in the documentation for __repr__, for example. The use of
the word "should" is not rigidly consistent throughout the document.
In other places, it is closer to the meaning of MUST in RFC 2119:
The reason such guides are in the reference manual is because ...
well, where else are you going to put them? Describing the use case of
a feature is, in this case I think, essential to describing the
feature. You have to explain why one would bother defining __iadd__ if
one already had __add__. Similarly, you have to explain why one would
use __repr__ over __str__ or vice versa.
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the Numpy-discussion