[issue9314] inconsistent result when concatenating list with iterators

Alexander Belopolsky report at bugs.python.org
Wed Jul 21 16:35:21 CEST 2010


Alexander Belopolsky <belopolsky at users.sourceforge.net> added the comment:

On Wed, Jul 21, 2010 at 2:09 AM, Ray.Allen <report at bugs.python.org> wrote:
..
> Does this means "a += b" is not the same as "a = a + b"?

For immutable a, the two are practically the same, for mutable, they
are necessarily different.   This is explained in __iadd__
documentation:

"""
These methods are called to implement the augmented arithmetic
assignments (+=, -=, *=, /=, //=, %=, **=, <<=, >>=, &=, ^=, |=).
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). If a specific method is not defined, the augmented assignment
falls back to the normal methods. For instance, to execute the
statement x += y, where x is an instance of a class that has an
__iadd__() method, x.__iadd__(y) is called. If x is an instance of a
class that does not define a __iadd__() method, x.__add__(y) and
y.__radd__(x) are considered, as with the evaluation of x + y.
"""  http://docs.python.org/reference/datamodel.html?#object.__iadd__

> I'd think this can be seen as a pitfall for python.

No.  Please see PEP 203 for the rationale.
http://www.python.org/dev/peps/pep-0203/

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9314>
_______________________________________


More information about the Python-bugs-list mailing list