__iadd__ and fellows missing (Python 2.2)

Ralf Juengling juenglin at informatik.uni-freiburg.de
Wed Apr 24 12:17:34 EDT 2002


Jeff Shannon <jeff at ccvcorp.com> writes:

> 
> > [Cut out from PEP 203]
> > 
> >       They implement the same operator as their normal binary form,
> >       except that the operation is done `in-place' when the left-hand
> >       side object supports it...
> 
> The key point here being "when the left-hand side object supports 
> it" -- which clearly implies that immutable objects will not be 
> modified in place, since they do not support modification.
> 
> While there might be some theoretical argument for considering 
> augmented assignment on immutable types to be an error, keep in 
> mind that practicality beats purity.  Making augmented assignment 
> on integers into an error would, in practice, be quite silly.

The practical argument for purity is, that consistent semantics of 
operations is easier getting used to. Even if the type supporting
 __iadd__ is mutable, an in-place operation is not demanded by the
language spec. It reads there

"... These methods should attempt to do the operation in-place
(modifying self) and return the result (which could be, but 
does not have to be, self)..."

I've realized that Python is much less strict than other 
languages and builds more often on convention rather than 
specification. It's probably just a matter of taste that I 
don't appreciate Python's freedom in any case (here I don't).

Thanks for your comments,
Ralf

PS: I also would miss the idiom "i+=1" for integer increments...



More information about the Python-list mailing list