Mutable class attributes are shared among all instances, is it normal ?

Donn Cave donn at u.washington.edu
Fri Jun 8 16:08:32 EDT 2001


Quoth Chris Barker <chrishbarker at home.net>:
...
| I think the point is not that self.x is mutable, but that self.y is NOT
| mutable. In other languages, x points to a particular piece of memory,
| and if you write x = somethingnew, that same memory is being used. In a
| number of begining programming texts I have seen variables described as
| boxes that numbers can be put in. Python mutable types can be boxes, and
| their contents changed without changing the box intself, but python
| imutable types are NOT boxes, so the distinction is important.

Not for me.  I can think of two contexts where mutable vs. immutable
is an important distinction -
  - the list, vs. the tuple, which are similar but the former supports
    a modify operation and hence is "mutable".
  - dictionary keys, where mutability as such is indeed a big issue.

The only reason to care what "mutable" means is the second one.

The beginning Python programmer probably ought to stay away from boxes
anyway.  Can an immutable type be a box?  Depends on what it means to
be a box, and what it means to put something in the box.  If the text
goes on to show you how to put value "a" in box "x" with "x := a", then
you lose, Python does not have boxes, period.  Even if it somehow skirts
that, what about retrieval - if it isn't a box, how come you can get
stuff out of it?  Bleah.  Maybe boxes work so well for so many people
that it's worth the trouble to make a box model of Python, but I fail
to see how mutability can help with that.

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list