Legends ofPythos
Claim your name
Objects and Classes

Class attributes versus instance attributes

Lesson 5 of 13

Watch the lesson5:33 · with Torsten
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 = name
Defining a class attribute
species 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 familiaris
Accessing the shared value
You 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)   # 2
Counting instances with a class attribute
type(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.

Your turn

0 of 2 solved

Exercise 1

+35 XP
Create a Player class with a class attribute team set to 'Red'. Give it an __init__ that takes name and stores it on the instance as self.name. Then create two players: alice = Player('Alice') and bob = Player('Bob').
Write your Python here.

Run your code to check it against the tests.

Exercise 2

+35 XP
Create a Counter class that counts how many instances have been created.

Use a class attribute called count, starting at 0. In __init__, add 1 to it with type(self).count += 1. Don't use self.count += 1: that gives the new object a count of its own, which hides the shared one, and Counter.count stays at 0.

After your code, create three counters: c1 = Counter(), c2 = Counter(), and c3 = Counter().
Write your Python here.

Run your code to check it against the tests.