Augmented Assignment in Python (PER)

Delaney, Timothy tdelaney at avaya.com
Tue Jul 17 22:30:58 EDT 2001


> "For the built-in types, augmented assignment doesn't violate
> mutability or perform in-place modification of objects. Therefore,
> writing x+=y creates an entirely new object x with the value x + y.
> User defined types and classes may choose to implement different
> behavior by redefining special methods such as __iadd__(), 
> __isub__(), 
> and so forth."
> 
> Note: I still stand by the statement for built-in types.  I 
> looked through
> the Python sources when writing the book and couldn't find any use of
> augmented assignment for built-in types.  I am not aware of any
> counter-example where x += y is not equal to x = x + y for 
> the standard
> built-in types (if there is such an example, please enlighten me!).

This is very very wrong.

a = []
b = a

print b
a += [1, 2]
print b

List is a built-in type. Lists are modified in-place by augmented
assignment.

The information in your book will lead to bad programming errors.

Tim Delaney




More information about the Python-list mailing list