On Feb 28, 11:24 am, Alan Franzoni <[EMAIL PROTECTED]> wrote: > Il 27 Feb 2007 16:14:20 -0800, [EMAIL PROTECTED] ha scritto: > > > > > Those mean different things: > > >>>> a=[1] > >>>> b=a > >>>> a += [2] > >>>> a > > [1, 2] > >>>> b > > [1, 2] > >>>> a=[1] > >>>> b=a > >>>> a = a + [2] > >>>> a > > [1, 2] > >>>> b > > [1] > > This is a really nasty one! I just answered to Tim above here, and then I > saw your example... I had never realized that kind of list behaviour. > That's probably because i never use + and += on lists (i prefer the more > explicit append() or extend() and i do copy list explictly when I want to) > , BTW I think this behaviour is tricky!
Seems obvious and desirable to me. Bare "=" is the way you assign a name to an object; saying "NAME =" will rebind the name, breaking the connection between a and b. Without it, they continue to refer to the same object; extending the list (via += or .extend) mutates the object, but doesn't change which objects a and b are referencing. It would be very counterintuitive to me if "=" didn't change the name's binding, or if other operators did. -- http://mail.python.org/mailman/listinfo/python-list