Instance, Class & Static Methods in Python
Python has three kinds of methods, each receiving different implicit information: an instance method (the default) automatically receives the specific instance it was called on as self; a class method, marked with @classmethod, receives the class itself as cls instead; a static method, marked with @staticmethod, receives neither, behaving like a plain function that simply lives inside the class for organization.
Instance methods take self
Giving access to that specific instance’s data.
@classmethod methods take cls
Giving access to the class itself, not any particular instance.
@staticmethod methods take neither
They don’t need instance or class data at all.
Class methods make good alternative constructors
Building and returning a new instance in a different way than __init__() does.
Static methods group related utilities
Purely for organizing a standalone helper function inside the class it logically belongs to.
class Pizza: def __init__(self, toppings): self.toppings = toppings @classmethod def margherita(cls): return cls(["tomato", "mozzarella"]) @staticmethod def is_valid_topping(topping): return topping in ["tomato", "mozzarella", "basil", "pepperoni"] p1 = Pizza.margherita()print(p1.toppings)print(Pizza.is_valid_topping("pineapple"))Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Python yourself — building beats watching.