On 27 February 2014 16:14, Chris Angelico <ros...@gmail.com> wrote: > On Fri, Feb 28, 2014 at 3:01 AM, Eric Jacoboni <eric.jacob...@gmail.com> > wrote: >> >>>>> a_tuple = ("spam", [10, 30], "eggs") >>>>> a_tuple[1] += [20] >> Traceback (most recent call last): >> File "<stdin>", line 1, in <module> >> TypeError: 'tuple' object does not support item assignment >> >> Ok... I accept this message as += is a reassignment of a_tuple[1] and a >> tuple is immutable... >> >> But, then, why a_tuple is still modified? > > This is a common confusion. > > The += operator does two things. First, it asks the target to please > do an in-place addition of the other operand. Then, it takes whatever > result the target gives back, and assigns it back into the target. So > with a list, it goes like this: > >>>> foo = [10, 30] >>>> foo.__iadd__([20]) > [10, 30, 20] >>>> foo = _
Would it be better to add a check here, such that if this gets raised to the top-level it includes a warning ("Addition was inplace; variable probably mutated despite assignment failure")? -- https://mail.python.org/mailman/listinfo/python-list