Is this a bug?

Robert Kern rkern at ucsd.edu
Sun Apr 24 23:49:08 EDT 2005


Grant Edwards wrote:
> On 2005-04-25, Terry Reedy <tjreedy at udel.edu> wrote:
> 
>>>   According to the language reference,
>>>
>>>     An augmented assignment expression like x += 1 can be
>>>     rewritten as x = x + 1 to achieve a similar, but not
>>>     exactly equal effect. In the augmented version, x is only
>>>     evaluated once.
>>>
>>>   I don't consider the two results you posted "similar".
>>
>>It continues
>>"Also, 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. ...Similarly, with the exception of the 
>>possible in-place behavior, the binary operation performed by augmented 
>>assignment is the same as the normal binary operations.
>>"
>>I take the behavior observed to be the exceptional in-place behavior 
>>referred to.
> 
> 
> I certainly don't see how.  Strings are immutable.  The old
> object can't be modified in-place, so the "in-place" behavior
> is moot.

It's the left-hand-side, in this case a list, that gets modified 
in-place. Whether the right-hand-side is mutable or not is irrelevant.

> In any case, the only difference is supposed to be whether a
> new object is created or an existing object is modified.  The
> two results shouldn't be completely different as shown by the
> OP.  
> 
> Your quote states quite clearly that the binary operation *is
> the same* whether it's spelt a = a + b or a += b.  That is
> simply not true for the example we're discussing.

No, the quote says "with the exception of the possible in-place 
behavior, the binary operation performed by augmented assignment is the 
same as the normal binary operations." This is "in-place" behavior. 
Badly designed "in-place" behavior, yes.

>>But this could certainly be clearer.
> 
> I don't see how that statement has anything to do with the bug
> at hand.

It's a mistake, but it's been in the wild too long to be changed. Thus, 
it should be documented.

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter




More information about the Python-list mailing list