Augmented Assignement (was: Re: PEP scepticism)

Steve Holden sholden at holdenweb.com
Fri Jun 29 14:00:22 EDT 2001


"Mark 'Kamikaze' Hughes" <kamikaze at kuoi.asui.uidaho.edu> wrote in ...
> 28 Jun 2001 11:32:34 -0700 in
<mailman.993753043.27848.python-list at python.org>,
> Paul Prescod <paulp at ActiveState.com> spake:
> > I think Python has been getting better and better as time goes on. But I
> > do have a concern about augmented assignment. How can a newbie
> > understand that tuples are "immutable" if this works:
> > mytuple += (foo,bar)
>
>   What would you expect from "mytuple = mytuple + (foo,bar)", and why
> would this be different from "mytuple += (foo,bar)"?
>
We would expect these to be the same.

> > Worse, once they've learned that this does NOT affect other references
> > to mytuple they are bound to be confused by
> > mylist += [foo, bar]
> > which does!
>
>   What would you expect from "mylist = mylist + [foo, bar]", and why
> would this be different from "mylist += [foo, bar]"?
>
The two statements *can indeed* have different results. The difference lies
in the apparent changes to other names bound to the same object:


>>> lst1 = ['one', 'two']
>>> lst2 = ['two', 'three']
>>> steveslist = lst1
>>> markslist = lst2
>>> lst1 = lst1 + ['new', 'list', 'items']
>>> lst2 += ['new', 'list', 'items']
>>> steveslist
['one', 'two']
>>> markslist
['two', 'three', 'new', 'list', 'items']

*Now* do you get it?

> > I've seen this confusion already -- in a draft of a book no less.
>
>   I'm confused by your confusion.  However, it looks like that author
> should probably make this more clear.
>
Despite your questions, mutable object behavior does vary between explicit
and augmented assignment, although immutable object behavior does not and
can not.

regards
 Steve
--
http://www.holdenweb.com/






More information about the Python-list mailing list