Augmented Assignement (was: Re: PEP scepticism)

Marcin 'Qrczak' Kowalczyk qrczak at knm.org.pl
Sat Jun 30 07:59:35 EDT 2001


Fri, 29 Jun 2001 20:39:48 +0400 (MSD), Roman Suzi <rnd at onego.ru> pisze:

> In fact, many things depend on the type of the objects. Types of the
> objects define what operation will be done. For example, in a simple
> 
> A = A + 1
> 
> + operation is different for different A type.

But it never modifies A, and always returns an object which has a
type similar to the type of A (not necessarily exactly, numeric types
can be promoted, but it won't return a string if the argument is a
dictionary for example).

I treat as a wart the fact that augmented assignment has a subtle
difference between mutable and immutable objects.

Both flavors of appending make sense in isolation. But in 2/3 of cases
the programmer must be aware which flavor is used. Using a wrong
flavor leads to a subtle error when an object is modified where it
should not be, or vice versa.

The remaining 1/3 is where it doesn't matter which variant is chosen:
where the old reference would be discarded anyway, so indeed +=
sometimes gives the best implementation for the given type.

-- 
 __("<  Marcin Kowalczyk * qrczak at knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^                      SYGNATURA ZASTĘPCZA
QRCZAK



More information about the Python-list mailing list