
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

When you read d.kind, Python does a instance -> class search for that attribute to find the attribute in question. That way an instance can always read a class variable using self.<name>. But you then attempted to change the 'class variable' by using d.kind = 'felis sylvestris' But that doesn't change the class variable, it creates a `kind' attribute on the 'd' instance which is now disconnected from the Dog.kind class variable. Had you done: Dog.kind = 'felis sylvestris' You would have found that e.kind and d.kind would have stayed in-step as they both would still refer to Dog.kind. ------ Original Message ------ From: "Sean Carter" <fallingfrog@gmail.com> To: docs@python.org Sent: Monday, 20 Dec, 21 At 20:19 Subject: [docs] documentation error? 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 <http://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 <http://d.name> , e.name <http://e.name> ) d.kind = 'felis sylvestris' print(d.kind, e.kind, d.name <http://d.name> , e.name <http://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 _______________________________________________ docs mailing list -- docs@python.org To unsubscribe send an email to docs-leave@python.org https://mail.python.org/mailman3/lists/docs.python.org/ <https://mail.python.org/mailman3/lists/docs.python.org/> Member address: anthony.flury@btinternet.com -- <br>Anthony Flury<br>anthony.flury@btinternet.com

When you read d.kind, Python does a instance -> class search for that attribute to find the attribute in question. That way an instance can always read a class variable using self.<name>. But you then attempted to change the 'class variable' by using d.kind = 'felis sylvestris' But that doesn't change the class variable, it creates a `kind' attribute on the 'd' instance which is now disconnected from the Dog.kind class variable. Had you done: Dog.kind = 'felis sylvestris' You would have found that e.kind and d.kind would have stayed in-step as they both would still refer to Dog.kind. ------ Original Message ------ From: "Sean Carter" <fallingfrog@gmail.com> To: docs@python.org Sent: Monday, 20 Dec, 21 At 20:19 Subject: [docs] documentation error? 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 <http://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 <http://d.name> , e.name <http://e.name> ) d.kind = 'felis sylvestris' print(d.kind, e.kind, d.name <http://d.name> , e.name <http://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 _______________________________________________ docs mailing list -- docs@python.org To unsubscribe send an email to docs-leave@python.org https://mail.python.org/mailman3/lists/docs.python.org/ <https://mail.python.org/mailman3/lists/docs.python.org/> Member address: anthony.flury@btinternet.com -- <br>Anthony Flury<br>anthony.flury@btinternet.com
participants (2)
-
anthony.flury
-
Sean Carter