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.
Table of Contents
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:
__init__()
is also an instance method. It is used to initialize the object. It gets called automatically every time when we create the object.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.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 by self
.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:
@staticmethod
to tell the Python that this method is the static method.self
parameter.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:
@classmethod
. When you write @classmethod
above any method, it tells the Python that this is the class method.self
is not required. But it uses cls
parameter to access the class variables and static methods.Summarising some important points to distinguish these methods:
cls
as the first parameter. A static method does not require any parameter to be passed.If you have any questions or any further confusion over instance vs static vs class method in Python, write to me in the comment.
should probably be
I really appreciate the way you identified those mistakes and informed us. Corrected!