Better Programming

Advice for programmers.

Follow publication

Member-only story

What Are @classmethod and @staticmethod in Python?

Become a method master by learning the three method types of a class

Artturi Jalli
Better Programming
Published in
4 min readJul 12, 2021

Class methods in Python
Photo by Chris Ried on Unsplash.

In Python, a class can have methods, class methods, and static methods. But what is the difference between these three? How do they work?

Today, you are going to learn the key aspects and differences between the class-specific methods via useful examples.

Methods in Python

In Python, a method is a function that belongs to an object. A method is implemented inside a class and it takes the self instance as an argument.

For example, a Weight class can have a pounds() method:

And you can call this method on a Weight object:

m = Weight()
print(m.pounds())

Output:

22.05

Class Methods in Python

A class method is useful when you need a method that is not specific to an instance but involves the class somehow.

Responses (1)

Write a response