diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst --- a/Doc/faq/programming.rst +++ b/Doc/faq/programming.rst @@ -1103,6 +1103,34 @@ result = [obj.method() for obj in mylist] +Why does aTuple[0] += 'item' raise an exception? +------------------------------------------------ + +Because '+=' is an assignment operator and tries to +update the, read-only, tuple. + +When the added-to item is a list the update of this list +will succeed:: + + >>> aTuple = ([],) + >>> aTuple[0] += [1, 2] + Traceback (most recent call last): + File "", line 1, in + TypeError: 'tuple' object does not support item assignment + >>> aTuple + ([1, 2],) + +The list is updated because the '+=' operator is more or +less equivalent to:: + + >>> tmp = aTuple[0] + >>> tmp.__iadd__([1,2]) + >>> aTuple[0] = tmp + +The call to *__iadd__* succeeds, but the update of +*aTuple* fails. + + Dictionaries ============