You have already learned how to store data inside
__init__ using self. These are instance attributes. Each object gets its own unique copy, which is perfect for things like a name or an age that differ from one object to the next.But what if you have data that should be exactly the same for every single instance of your class? For example, all dogs are
Canis familiaris. You do not want each dog to carry its own copy of this fact. Instead, you can store it directly on the class itself.class Dog:
# Class attribute: shared by all dogs
species = 'Canis familiaris'
def __init__(self, name):
# Instance attribute: each dog's own
self.name = namespecies is a class attribute. It lives on the class object itself. When you create two different dogs, they both point to this single shared value.class Dog:
species = 'Canis familiaris'
def __init__(self, name):
self.name = name
rex = Dog('Rex')
bella = Dog('Bella')
print(rex.species) # Canis familiaris
print(bella.species) # Canis familiarisYou might wonder why you would use a class attribute instead of just putting it in
__init__. The main reason is clarity and memory. It tells other programmers, "This value defines the type, not the individual." If every instance needs to know its team name or default settings, a class attribute keeps your code clean.Now, consider a common problem: counting how many objects have been created. You could try using an instance attribute to store the count, but each new object would start with its own counter at zero or one. That does not give you the total number of all instances.
A class attribute is perfect for this, because there's only one of it, shared by every object. When
__init__ runs, each new object can add one to it.class Counter:
# Class attribute: one count for all
count = 0
def __init__(self):
type(self).count += 1
c1 = Counter()
c2 = Counter()
print(Counter.count) # 2type(self) gives the object's class, here Counter, so type(self).count += 1 adds one to the one shared count. Every new object updates the same number, so after two counters, Counter.count is 2.Don't write
self.count += 1 here. Setting a value through self gives that one object a count of its own, which hides the shared one, and Counter.count stays at 0.