Difference between @classmethod, @staticmethod, and instance methods in Python.

Learn what's the difference between a class method, a static method, and an instance method in Python.

In Python you will finde some methods decorated with @staticmethod or with @classmethod, but what exactly will they do?

Let's look at an example to show the difference:

class SoftwareEngineer: alias = "Keyboard Magician" # this is a class variable def __init__(self, name): self.name = name # this is an instance variable # this is an instance method def code(self, language): print(f"instance method, {self.name} aka {self.alias} codes in {language}") @classmethod def class_code(cls, language): print(f"class method, {cls.alias} codes in {language}") # name cannot be accessed! @staticmethod def static_code(language): print(f"static method, codes in {language}") # name and alias cannot be accessed! def global_code(language): print(f"global function, codes in {language}")

Too call the instance method code(self, language), we first need to create an instance of the class SoftwareEngineer.

For both the class method and the static method we can call the corresponding function either on the instance or on the class itself by saying SoftwareEngineer.class_code("Python") or SoftwareEngineer.static_code("Python"), respectively. The output is the same for both ways of calling the function.

se = SoftwareEngineer("Patrick") se.code("Python") # SoftwareEngineer.code("Python") --> Error! not possible # --> instance method, Patrick aka Keyboard Magician codes in Python se.class_code("Python") SoftwareEngineer.class_code("Python") # --> class method, Keyboard Magician codes in Python se.static_code("Python") SoftwareEngineer.static_code("Python") # --> static method, codes in Python global_code("Python") # --> global function, codes in Python

Notice that for the instance method we do not put in self, and for the class method we do not put in cls in the function call. These arguments are implicitely passed for us!

Instance methods

Instance methods take the argument self and can therefore access all instance variables and methods like self.name, and also all class variables and methods like self.alias here.

They can only be used on instances and not on the class directly.

Class methods

Instance methods take the argument cls and can therefore access all class variables and methods like cls.alias, but no instance variables/methods.

Use this when you don't need to access variables that belong to an instance, but still need general attributes that belong to the class.

Static methods

Static methods can neither access class variables/methods nor instance variables/methods. They behave like plain (global) functions except that you can call them from an instance or the class.

Why would you use it then?

Sometimes it makes sense to put code into a class as @staticmethod because it logically belongs with the class. By calling a static method from the class, we also combine it with a namespace. So when calling it we immediately see that it belongs to "SoftwareEngineer".

If you want to learn even more, you can take a look at my Object Oriented Programming (OOP) Beginner Crash Course.

FREE VS Code / PyCharm Extensions I Use

🪁 Code faster with Kite, AI-powered autocomplete: Link *

✅ Write cleaner code with Sourcery, instant refactoring suggestions: Link *

* These are affiliate links. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you! 🙏

Check out my Courses