0
0
Pythonprogramming~10 mins

Class methods and cls usage in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Class methods and cls usage
Define class with @classmethod
Call class method on class or instance
cls parameter receives the class
Access or modify class variables via cls
Return or print results
End
Class methods receive the class itself as the first argument (cls), allowing access to class variables and methods without needing an instance.
Execution Sample
Python
class Dog:
    count = 0

    def __init__(self, name):
        self.name = name
        Dog.count += 1

    @classmethod
    def how_many(cls):
        return cls.count

print(Dog.how_many())
d1 = Dog('Max')
d2 = Dog('Bella')
print(Dog.how_many())
This code counts how many Dog instances are created using a class method that accesses a class variable.
Execution Table
StepActioncls valueClass variable countOutput
1Call Dog.how_many()Dog00
2Create d1 = Dog('Max')Dog1
3Create d2 = Dog('Bella')Dog2
4Call Dog.how_many()Dog22
5End---
💡 No more code to execute, program ends
Variable Tracker
VariableStartAfter 1After 2Final
Dog.count0122
d1.name-MaxMaxMax
d2.name--BellaBella
Key Moments - 3 Insights
Why does the class method use cls instead of self?
cls refers to the class itself, not an instance. This allows the method to access class variables like count, as shown in execution_table steps 1 and 4.
Can we call the class method on an instance like d1.how_many()?
Yes, but cls will still refer to the class Dog, not the instance. The method accesses class variables, so it works the same as calling Dog.how_many().
Why does Dog.count increase when creating instances?
In __init__, Dog.count is incremented each time a new Dog is created, updating the class variable tracked by the class method how_many().
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of Dog.count after creating d2?
A1
B2
C0
D3
💡 Hint
Check the 'Class variable count' column at step 3 in the execution_table.
At which step does Dog.how_many() return 0?
AStep 4
BStep 2
CStep 1
DStep 3
💡 Hint
Look at the 'Output' column in the execution_table for when how_many() is called.
If we add a third Dog instance, what will Dog.count be after creation?
A3
B2
C1
D4
💡 Hint
Refer to variable_tracker for Dog.count increments after each instance creation.
Concept Snapshot
Class methods use @classmethod decorator.
They receive cls as first parameter, the class itself.
Use cls to access or modify class variables.
Called on class or instance, cls always refers to class.
Useful for tracking data shared by all instances.
Full Transcript
This example shows how class methods work in Python. The class Dog has a class variable count that tracks how many dogs are created. The __init__ method increases count each time a new Dog instance is made. The class method how_many uses cls to access the class variable count and returns it. We call Dog.how_many() before and after creating instances to see the count change. cls always refers to the class Dog, not any instance. This lets class methods access shared data easily.