Instance vs Static vs Class Method in Python | Explained with Code
In this tutorial, we are going to learn about the instance, static and class methods in Python. Whether you want to excel in Python programming or preparing for an interview, this is the concept you should be aware of.
Prior to that, you should be familiar with the classes and objects in Python.
Syntax to define the class in Python:
#define class
class sample:
pass
#creating object of the class
obj = sample()
Note: We have not defined anything inside the class, so we used the pass
keyword.
Inside the class, we can define the multiple variables and methods.
There are different types of the method we can define inside the class- instance methods, static methods and class method. All these methods have different purposes and use cases.
Let’s see one by one.
Instance Method in Python
This is the most common method we define inside the class. This method defines the behavior of the object (not the class).
Suppose you define a class for Bikes. Bikes have different model numbers, colors, and registration numbers. These are the properties of the class. An object created from this class can store information about a single bike. You can create multiple objects (instances) to store information about each bike.
Below is an example of an instance method (myFunc()
).
Python code for instance method:
class sample:
def __init__(self, val):
self.val=10
#instance method
def myFunc(self):
print("Value: ", self.val)
obj = sample(10)
obj.myFunc()
Output:
Value: 10
Important points to remember:
- The method
__init__()
is also an instance method. It is used to initialize the object. It gets called automatically every time when we create the object. - We can create multiple objects for a single class. Here each object will be holding different data. Instance methods are used to define the property for each object (called an instance).
- The first parameter of the instance method is
self
. It is a special Python term for the current object. You don’t need to pass this object explicitly while calling the instance method. - Just like the instance method, we can also define the instance variable. The variable
val
is an instance variable. It holds different values for each object. To access any instance variable inside the instance method, it should be followed byself
.
Static Method in Python
It’s a special type of method. Though we define the static method inside the class, it does not (cannot) access any class or instance variable.
It means, it is a self-content method. It can only access the data which is passed as parameters to the method.
To define any method as static, you need to define a Python decorator.
Below is an example of a static method in Python.
Python code for the static method:
class sample:
def __init__(self, val=5):
self.val=10
#static method
@staticmethod
def myFunc():
print("This is the static method.")
sample.myFunc
Output:
This is the static method.
Important points to remember:
- You have to use the decorator
@staticmethod
to tell the Python that this method is the static method. - Unlike the instance method, you don’t use
self
parameter. - This function can not access any instance variables or methods. Static methods can only use the values passed as an argument while calling them. It is a self-content function.
- It is more like a regular method defined inside the class.
Class Method
The class method defines the behavior of the entire class (not the object). This method is common for all class instances.
class sample:
#class variable
varClass = 20
#class method
@classmethod
def myFunc(cls):
print("Inside class method.")
print("Class variable value: ", cls.varClass)
cls.otherFunc()
#static method
@staticmethod
def otherFunc():
print("Inside static method.")
sample.myFunc()
Output:
Inside class method. Class variable value: 20 Inside static method.
In the above example, I have demonstrated accessing class variable and static methods from the class method.
Important points to remember:
- To define the class methods, use decorator
@classmethod
. When you write@classmethod
above any method, it tells the Python that this is the class method. - A class method can not access data from any specific instance or object.
- It can access any class variable or static method.
- Here, the
self
is not required. But it usescls
parameter to access the class variables and static methods. - To access the class methods, we don’t even need to create the object of the class. It can be accessed simply using the class name.
[Difference between] Instance vs Static vs Class Method in Python
Summarising some important points to distinguish these methods:
- The instance method uses the self as the first parameter. The class method uses the
cls
as the first parameter. A static method does not require any parameter to be passed. - The instance method defines the behavior of the object (instance). The class method defines the behavior of the class.
- Static and class method requires a decorator. Instance method does not require any decorator.
- Unlike the instance and class method, the static method cannot access any class members.
- The difference between static and class method is one of the most common questions asked in the Python interview.
If you have any questions or any further confusion over instance vs static vs class method in Python, write to me in the comment.
Comments
Kyle Sallee
should probably be
Aniruddha Chaudhari
I really appreciate the way you identified those mistakes and informed us. Corrected!