Augmented Assignement (was: Re: PEP scepticism)

Guido van Rossum guido at python.org
Sun Jul 1 16:12:54 CEST 2001


Paul Prescod <paulp at ActiveState.com> writes:

> It is much clearer in isolation but not when placed in the context of
> the rest of the language. It has different behavior than "x = x + y"
> which is what most people will think x += y means. Someone even posted
> on this thread: "I don't understand what's so complicated x+=y is the
> same as x = x + y".

In most situations the aliasing possibility that makes these two
different in semantics is not an issue (because there is no alias to
the variable x), and then they can safely be considered equivalent.

Python's rules for immutable objects have always encouraged fuzzy
thinking abou variables: immutable types were invented so that folks
who focus on variables rather than the objects referred to by those
variables won't encounter surprises, as long as they use immutable
types (particularly, numbers and strings).  But then the introduction
of mutable objects (lists and dictionaries) unavoidably has to create
some confusion.

Like the introduction of pointer types in other languages, once you
have the right mental model it shouldn't be a problem.

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-list mailing list