[issue19953] __iadd__() doc not strictly correct

ferno report at bugs.python.org
Wed Dec 11 21:13:08 CET 2013


New submission from ferno:

Document in question is:

    http://docs.python.org/3.3/reference/datamodel.html#object.__iadd__

The documentation states,

    to execute the statement x += y, where x is an instance of a class that has an __iadd__() method, x.__iadd__(y) is called.

However, this doesn't appear to be strictly true. According to this, the following well-known example:

    >>> a = (1, [2, 3])
    >>> a[1] += [4, 5]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'tuple' object does not support item assignment
    >>> a
    (1, [2, 3, 4, 5])

should give the same behaviour as:

    >>> a = (1, [2, 3])
    >>> a[1].__iadd__([4, 5])
    [2, 3, 4, 5]
    >>> a
    (1, [2, 3, 4, 5])

However, this snippet DOES give the identical behaviour:

    >>> a = (1, [2, 3])
    >>> a[1] = a[1].__iadd__([4, 5])
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'tuple' object does not support item assignment
    >>> a
    (1, [2, 3, 4, 5])

which leads me to suggest that this line of the documentation should be adjusted to read:

    to execute the statement x += y, where x is an instance of a class that has an __iadd__() method, x = x.__iadd__(y) is called.

This fix would incidentally harmonise with the documentation for operator.iadd() et al., (http://docs.python.org/3.3/library/operator.html#operator.iadd), which had a similar problem but was corrected following issue 7259 (http://bugs.python.org/issue7259), now reading:

    a = iadd(a, b) is equivalent to a += b.

----------
assignee: docs at python
components: Documentation
messages: 205920
nosy: docs at python, ferno
priority: normal
severity: normal
status: open
title: __iadd__() doc not strictly correct
versions: Python 2.6, Python 2.7, Python 3.1, Python 3.2, Python 3.3, Python 3.4, Python 3.5

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


More information about the Python-bugs-list mailing list