On Oct 10, 8:23 am, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > > However, it is not true that += "always leads to a rebinding of a to the > > result of the operation +". The + operator for lists creates a new list. > > += for lists does an in-place modification: > > It still is true. > > a += b > > rebinds a. Period. Which is the _essential_ thing in my post, because > this rebinding semantics are what confused the OP.
Doesn't this depend on wether "a" supports __iadd__ or not? Section 3.4.7 of the docs say """ If a specific method is not defined, the augmented operation falls back to the normal methods. For instance, to evaluate the expression 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. """ So if a.__iadd__ exists, a += b is executed as a.__iadd__(b), in which case there's no reason to rebind a. However, this confuses the heck out of me: >>> class A: ... l = [] ... >>> class B(A): pass ... >>> B.__dict__ {'__module__': '__main__', '__doc__': None} >>> B.l [] >>> B.l.append('1') >>> B.l ['1'] >>> B.__dict__ {'__module__': '__main__', '__doc__': None} >>> B.l.__iadd__('2') ['1', '2'] >>> B.l ['1', '2'] >>> B.__dict__ {'__module__': '__main__', '__doc__': None} >>> B.l += '3' >>> B.__dict__ {'__module__': '__main__', '__doc__': None, 'l': ['1', '2', '3']} Why is B.l set for the += case only? B.l.__iadd__ obviously exists. Paul -- http://mail.python.org/mailman/listinfo/python-list