preferred syntax for list extend?
Bernhard Herzog
bh at intevation.de
Tue Apr 23 05:32:14 EDT 2002
Greg Ewing <greg at cosc.canterbury.ac.nz> writes:
> Bernhard Herzog wrote:
> >
> > Why would you want to implement __iadd__ for immutable objects?
>
> So that, e.g. i += 1 works when i is an integer.
You don't need __iadd__ for that and indeed the int implementation in
2.1.3 does not have the __iadd__ method (more precisely, its C-level
equivalent). The interpreter automatically falls back to using __add__
so that i += 1 becomes equivalent to i = i + 1. The only difference is
that in e.g. x[i] += 1, i is only be evaluated once, as I understand it
anyway.
An immutable object implementing __iadd__ to behave different than
__add__ is probably asking for trouble, although in some rare
circumstances it might offer some optimizations not possible with
__add__ but I can't think of one at the moment.
Bernhard
--
Intevation GmbH http://intevation.de/
Sketch http://sketch.sourceforge.net/
MapIt! http://www.mapit.de/
More information about the Python-list
mailing list