Home > Python > Python Methods – Instance, Class and Static Methods

Python Methods – Instance, Class and Static Methods

Today we are going to learn about different methods that we can use with the python class. That’s python methods are the instance method, class method, and static method.

So in this tutorial, we will have a look at what’s are python methods and what’s the difference between the instance method, class method, and the static method.

The instance methods are used to get or set details of object not the class. They have the first default parameter “self” and you can take many as you want. To access the instance method, you have to create the object of the class.

The python class methods are linked with class not the object of that class and it has default “cls” parameter. In order to use the class method, you have to define the @classmethod decorator. It is a built-in decorator function.

And the static method is also the same but there are some basic differences to define and use it. We will see below later with an example. To use the static method, you have to define the @staticmethod decorator.

Recommend tutorial: How to Create Nested Functions in Python?

What are Methods in Python?

In python, we have objects to access the functions of the class. These objects are made up of properties and actions. Attributes specify the object’s properties, whereas methods define the object’s action.

These methods are defined inside the class and these methods are pieces of reusable code that can be used at any time in the program.

A method is a block of code to carry out a specific task. Methods may contain zero(no) arguments or more than one argument and it is associated with object/classes. The method is implicitly used for an object and it is accessible to data that is contained within the class.

Python has many types of methods to use for different purposes. But there are three main types of methods in python that’s are very useful and we used them regularly in the python program.

Recommend tutorial: Python Sequences – Types, Operations, and Functions

Types of Methods in Python

  • Instance Method
  • Class Method
  • Static Method

Let’s have a look at the details of each method.

Python Instance Methods

The instance methods are used to get or set the attributes of the instance (object) of the class. They get the details of instances, that’s why they are known as instance methods. Instance methods are the most common types used in the python class.

They have one default “self” parameter, which refers to an instance of the class. You can change the name of this parameter and you don’t need to declare it in the method, python will do this for you.

Let’s see the example code of the instance method.

In the above example, we declare the method in the class and its returning simple text.

Now, if you want to call this method you have to create an instance of the class. By using this class instance, you can access the methods and attributes of the class.

Example:
Output:
Rahul | Sen | 20 | B

So that’s how you can create the instance methods and access them by creating the object of the class.

Recommend tutorial: Multiple Constructors For Class in Python

Python Class Methods (@classmethod)

The class methods (@classmethod) of python are a built-in decorator function. The purpose of the class methods is to set or get the details of the class. That is why we call class methods. The class method accepts the class itself as an implicit argument.

The class methods can’t access or modify specific instance data because they are bound to the class instead of their objects.

They can access the state of class with the default class (cls) parameters that will refer to a class, not to the object. You can also change the name of the parameter but I would suggest you take a simple name and go with conventions.

See the following example code of the class method.

The above class has a class method and it will return simple text. So let’s call this method by creating an instance of the class (As you know you can access any method with the help of the object of the class). So, for example, we will initiate the class ClassName and run the class_method() function.

As you see, we access the class method with the help of the class object.

But do you know what?

You can also access the class method directly without creating the object of the class. You don’t need to create the instance of the class. See the following code.

Example:
Output:
Rahul | Sen | 20 | A

Python Static Method (@staticmethod)

The python static method (@staticmethod) cannot access the class methods. They do not take an implicit first argument because they are self-sufficient and can work on their own. The static methods are not linked with any attribute of class and so they cannot access or modify the state of the class.

You can define the static methods with the help of the @staticmethod decorator function (same as we used @classmethod decorator).

So, as we see you need to pass the argument or default parameter in the instance method and class method but in the static method, you don’t need to pass any argument either default parameter.

Let’s see the example code of the static method.

In the above, you can see we haven’t passed any argument in the static method. Now we will call the static method using the object of class as we did in the above two methods (instance and class method).

We can also call the static method directly from the class without creating the instance of the class. See the following syntax of directly call static methods.

Conclusion

In this tutorial, we covered what are the python methods and see the difference between the static method, class method, and the instance method.

Instance methods have the default parameter “self” which refers to the object of the class. They are used to get or set the details of the object, not the class.

Class methods are used to access or modify the details of the class, not the object. They also have the default parameter “cls” that points to the class. To define the class method, you have to use the @sclassmethod decorator.

On other hand, the static methods are normal methods but you cannot access the data of the class. They are independent to work on their own functionality. To define the static method, you have to use the @staticmethod decorator.

Hope you understand the python methods and the difference between instance methods, class methods, and static methods. If you still have any questions please ask me in the comment section, I’ll respond to you as soon as possible.

More Python Tutorials:
1) Python range() Function – reverse, increment, decrement
2) How to Extract Data from JSON File in Python?
3) How to Test Internet Speed Using Python?
Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

Leave a Comment