Summary: In this tutorial, we will learn what are instance, class, and static methods in Python and how they work, and the difference between them.
Recommended:
Consider the following example in Python with all 3 types of methods (for reference):
class MyClass:
def instance_method(self):
print("Instance Method ", self)
@classmethod
def class_method(cls):
print("Class Method ", cls)
@staticmethod
def static_method():
print("Static Method")
Instance Method
Instance Method is a method that is bound to the instance of the class i.e. object.
It takes self
parameter as an argument which holds the reference to an instance of the class. That is why it can only be called using an object reference.
In our program, instance_method()
is an example of an instance method.
>>> obj = MyClass()
>>> obj.instance_method()
Instance Method <__main__.MyClass object at 0x7fc143ca2100>
Notice, we are not passing any arguments to the method. The reference of obj
is passed to the self
parameter underhood by the Python itself.
What if we call the instance method using the class reference?
>>> MyClass.instance_method()
Traceback (most recent call last):
File "<string>", line 18, in <module>
TypeError: instance_method() missing 1 required positional argument: 'self'
We get an error – the required self
argument is missing.
As I said, instance methods are bounded by instances. It needs a reference of an object as the self
parameter.
We can also pass the object reference manually to the instance method like the following:
>>> obj = MyClass()
>>> MyClass.instance_method(obj)
Instance Method <__main__.MyClass object at 0x7fd01c2b0100>
Instance methods can access both object and class properties. Using the self.__class__ attribute any class variable can be accessed inside the method.
This is why the instance method has more access compared to other types of methods.
Class Method
A method with ‘@classmethod‘ decorator in function declaration becomes a class method.
Class methods are bounded by the class not by an instance.
Instead of self
, class methods accept cls
parameter which holds the reference of the class.
self and cls are just naming convention, what matters is the reference it holds.
Ironically, class method can be called using both object and class reference:
>>> MyClass.class_method()
Class Method <class '__main__.MyClass'>
>>> obj = MyClass()
>>> obj.class_method()
Class Method <class '__main__.MyClass'>
Again, Python under the hood passes the class reference to the cls
parameter.
Since the class method does have access to the self
parameter, it cannot access instance properties. However, class methods can still access the class properties that are applicable to every instance of the class.
When to use Class Methods
Use the class method when you want to access the class state or variables inside the function.
Class method is the best option when the reference to class is required to fulfil the method’s task e.g. creating another object.
Static Method
We can make a method static using the ‘@staticmethod‘ decorator in the function declaration.
Neither self
(object instance) or cls
(class reference) are passed as an argument by the Python to the static method. Hence it cannot access or modify any class or instance properties.
Static method can be called using both class and object reference.
>>> MyClass.static_method()
Static Method
>>> obj = MyClass()
>>> obj.static_method()
Static Method
When to use Static Methods
Use the static method when you don’t require any class or instance properties inside the function to perform a particular task.
It is best suited for the task that is independent of every aspect of the class.
Conclusion
- Instance methods are regular methods inside the class that needs an object reference as
self
parameter. - Class methods are declared using the ‘@classmethod’ decorator and can only access the class state using
cls
parameter. - Static methods are declared using the ‘@staticmethod’ decorator and don’t have any access to class or object state.
In this tutorial, we learned what instance, class, and static methods are in Python. We discussed the significance of each method and the difference between them as well.