Methods are functions that are associated with a particular object or class. The three types of methods are :
- Instance methods: These methods are associated with instances of a class and can access and modify the data within an instance.
- Class methods: These methods are associated with a class rather than instances. They are used to create or modify class-level properties or behaviors.
- Static methods: These are utility methods that do not have access to any object-level or class-level data.
Understanding the difference between the 3 types of methods will be crucial in ensuring that you write classes that are properly organized, efficient, easy to maintain and which are a close reflection of the real life objects they are being modeled after.
The following example shows how each of these methods are defined in a class. We will talk about the technical details in a while.
Instance Methods
Instance methods are functions defined inside a class that perform operations on instances rather than the class itself.
If you have interacted with Python classes, it is likely that you have encountered and used instance methods. You can easily identify the methods by looking for the parameter self
in the method definition.
Instance methods are able to access and modify an instance variables and data.
Note: Calling an instance method from the class itself will raise a TypeError
.
modify class variables at instance level.
Through the self
parameter, we can update variables defined at class level but only for the current instance. This means that the variable will remain unchanged at class level(i.e for other instances).
Class Methods
Class methods are methods that are bound to a class rather than to objects. These methods can be called directly from the class itself as well as from the instances.
Class methods take cls
as the first argument. The cls
argument is a reference to the class, and is used to access class attributes and methods. It is analogous to the self
argument for instance methods.
We use the @classmethod decorator to define a class method. The syntax is as follows:
@classmethod def method_name(cls): #method body
copy
Class methods operate on the class itself, rather than on an instances of the class. They are used to provide general functionality for objects of that particular class.
Consider the following example:
The following example shows how we can keep a count of all the active instances of a class using class method.
Static methods
From the above discussions, instance methods operate on instances, while class method operate on the class itself.
Static methods, as the name suggests are just static. They belong to a class but the do not have access to class or instance variables. They are simply meant for providing utility services that are not class or instance specific.
Note: While instance method and class methods always take self
, cls
as their first arguments respectively. Static methods do not take any mandatory argument as the first argument. This is because they are not bound to either the class or instances and are simply called directly.
Static methods are defined using the @staticmethod decorator. The syntax is as follows:
@staticmethod def method_name(params): #method body
copy
As shown above, static methods can be called from the class itself as well as from the instances.
Consider if we have the class Rectangle, we can define a utility/static methods for calculating the area and the perimeter.
Summary
- Instance methods operate at instance level. They take
self
as their first argument - Class methods operate at class level, they have access to class variables. They are defined using the @classmethod decorator. They take
cls
as the first argument. - Static methods are utility methods, they do not have access to class or instance variables. They are defined using the @staticmethod decorator and they do not take any mandatory first argument.