Data sticking around too long

Stefan Schwarzer sschwarzer at sschwarzer.net
Thu Sep 7 14:14:43 EDT 2006


Hello Matimus,

On 2006-09-07 00:07, Matimus wrote:
> Someone correct me if I'm wrong (sometimes I get the terms mixed up)
> but I believe that what you are seeing is due to 'number' being an
> immutable type. This means that its value cannot be changed and thus
> each assignment is effectively creating a new instance if int.

The significant difference is not between mutable and immutable
objects, but between modifying an object in-place
(e. g. some_list.append(1)) and assigning an object to a name
(e. g. some_int = 1). If you do an assignment to a class
"variable", the binding to the previous object (if any) will be
removed and a new binding to the assigned object created.

The assignments Rob does with

c1.number = 1
c2.number = 2

create a new name "number" in each of the instances' namespaces
and assign the objects 1 and 2 to them, respectively. In contrast,
the list scanList is modified in-place with

c1.scanList.append("One")
c2.scanList.append("Two")

so no names are created in the instances' namespaces and both
operations modify the object in the class's namespace.

> I believe lists are considered immutable also

I guess you confuse this with tuples.

> but since it is a container
> type it behaves somewhat differently.

Again, this has nothing per se to do with being a container
though many containers are mutable objects and often are
modified in-place.

Stefan



More information about the Python-list mailing list