In section 9.3.5. Class and Instance Variables, it makes a distinction
between class and instance variables that doesn't look completely right.
It looks to me like what's going on is that class variables are bound when
the interpreter gets to that class, kind of like default parameters to
functions, and then a copy of the class variable is created when the class
is instantiated. BUT, in the case of a list, it's a shallow copy, so you
end up in a situation where every instance is aliased to the same place in
memory, which gives the *illusion* of a hard distinction between class and
instance variables. For example, try this:
class Dog:
kind = 'canine' # class variable shared by all instances
def __init__(self, name):
self.name = name # instance variable unique to each instance
d = Dog('Fido')
e = Dog('Buddy')
d.kind # shared by all dogs
print(d.kind, e.kind, d.name, e.name)
d.kind = 'felis sylvestris'
print(d.kind, e.kind, d.name, e.name)
The output will be:
canine canine Fido Buddy
felis sylvestris canine Fido Buddy
Which would not be the case if those were truly "class variables"
Sean