arbeite mich gerade durch Dive Into Python durch, und bin da in Kapitel 5.8 (Introducing class attributes) hängen geblieben:
Code: Alles auswählen
class counter:
count = 0
def __init__(self):
self.__class__.count += 1
>>> c = counter()
>>> d = counter()
>>> c.count
2
>>> counter.count
2
>>> c.count = 13
>>> d.count
2 # Wieso nicht 13?
>>> counter.count
2
>>> e = counter()
>>> c.count
13
Dive Into Python schreibt "notice how the class attribute is shared by the class and all instances of the class".
Wieso ändert sich dieses Verhalten, sobald ich von außerhalb das Klassenattribut ändere (c.count=13)?
Bis dahin schien es so, als ob counter.count, c.count und e.count nur unterschiedliche Namen für das selbe Objekt wären (bzw. so habe ich mir Klassenattribute vorgestellt), aber das stimmt offensichtlich nicht?