[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: assignment, reference, += and parameter passing

justin wrote:

> a += b
>   This is a bit funny.  Generally, if a refers to a mutable object such
>   as a list, this modifies the existing object that a refers to.  If a
>   refers to an immutable object such as an int, this rebinds a to
>   refer to a new object obtained by adding a and b.

Not really: if "a" refers to an object that defines an in-place add
method, it calls that method.  Otherwise, it uses the corresponding
binary operation (also a method).

Python itself doesn't distinguish between "mutable" and "immutable"
objects; behaviour is always defined by the objects themselves.

(in your example, the design mistake is probably to implement "+="
for lists to mean "extend".  guess someone got carried away...)