Augmented Assignment (was: Re: PEP scepticism)

Steve Holden sholden at holdenweb.com
Thu Jun 28 21:55:45 EDT 2001


"Rainer Deyke" <root at rainerdeyke.com> wrote in ...
> "David Bolen" <db3l at fitlinxx.com> wrote ...
> > I think that if the operation is phrased from the start as an action
> > undertaken by the source object itself and that the source object's
> > mutability can affect behavior, that it's not too big of a hurdle.
>
> This dual meaning of augmented assignment makes it dangerous: to get
> reliable behavior, you need to be absolutely sure whether or not your
object
> is mutable before you attempt augmented assignment.  I cringe in horror
when
> I think about people intentionally using the dual meaning of augmented
> assignment.
>
You might, but when I cringed (in this newsgroup) it was pointed out in no
uncertain terms (although perfectly politely, of course) that this had been
a part of the master plan for quite a long time, and was something that many
users wanted.

At the time it seemed dangerous (in pedagogical terms) to have it be the
case that

    var = var op exp

gave different results from

    var op= exp

Now, strangely enough, it's the *mutable* objects for which different
results are obtained, because if the object can implement the operation by
mutating then the value of other names also bound to the object changes
under the covers. So I had no problems with augmented assignment on tuples,
for example (which started this thread, IIRC).

Nowadays, since it's a part of the language, I just use it and don't moan.
But it does have to be taught...

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








More information about the Python-list mailing list