list.clear() missing?!?

Fredrik Lundh fredrik at pythonware.com
Sat Apr 15 07:06:59 EDT 2006


"Ben C" wrote:

> I used to think it assigned with things like integers, because if you
> write:
>
>     a = 5
>     b = a
>     b += 1
>     print a
>
> a is still 5. So it looked like a and b stored values and b got a "copy"
> of a's value. But this is the wrong interpretation,
>
>     b += 1
>
> is really b = b + 1, and rebinds b.

almost: "b += obj" is really "b = b.__iadd__(obj)" with "b = b + obj"
as a fallback if __iadd__ isn't supported by "b".

> If it weren't for the id() function I think the difference between
> "variable stores value", "variable stores immutable reference" and
> "variable stores copy-on-write reference" would be implementation
> detail and never visible to the programmer.

except when they are:

    >>> a = [1, 2, 3]
    >>> b = a
    >>> b += [4]
    >>> a
    [1, 2, 3, 4]
    >>> b
    [1, 2, 3, 4]

</F>






More information about the Python-list mailing list