Python Class Methods with Example

Now, we are going to learn about a very interesting concept, of Python Class Methods With Examples. Basically, we have been dealing with the classes on objects for a while, and we are familiar with the concept of function and methods (the method is simply a function, which belongs to some particular class like the sort method is for the list) as well. So now, it’s time for us to explore the class methods in python.

So far, the methods that we have been creating in some classes are the instance methods. Instance methods are basically used for the objects. Let’s say that we have a class Student, and we want to calculate the sum of marks of some particular student, and for that, we are creating a method in the Student class, so that would be an instance method. So, we can simply say that the instance methods are used to access or modify the object state. On the other hand, if we want to access or change the class state, then we have the class methods.

Python Class Methods

Let’s have a look at a simple example, through which, we can have a more clear idea about the instance methods,

As you can see in the above program, we have a class Student, and then we are having a class variable, which is college_name, and then we have the constructor. Also, we have a method, with the name message, which is an instance method, as we have been dealing with instance methods so far in our programs. So, we are creating the objects of the Student class, and then we are calling the message method for both objects, as seen in the above program.

So, the instance methods that we have in our program, are for accessing or changing the object state. But at times, we might require accessing or changing the state of the class. For example, Let’s say that we want to change the college name. So, it would be changed for all the students. So, in such a situation, we need to create the class method. But before that, Let’s try to access the class variable, and for that, we would use the class name.

As you can see in the above program, we have many things from the previous program, but this time, we are accessing the class variable, using the class name. Now, what if we want to change the name of the college? Well, this would be done for all the students. This is like if you are changing your name on some social media platform, it changes for everyone. So, for that, we can create a class method, so that we can access or change the class state.

But now the question is how are we going to create a class method? When we create an instance method, it is very similar to creating some function. Here as well, we have to create a similar method, and then to make it a class method, we can make use of a decorator, which is @classmethod. Alternatively, we can also use the classmethod() function. Let’s have a look at a simple program, where we would create a method to change the name of the college.

As you can see in the above program, we have many other things from the previous example, and also, we have the class method. As you can see that we have used the decorator @classmethod, so that the method becomes a class method. If you observe the class method change_college_name carefully, you can find that it takes an argument, cls, which is nothing but the class. Also, we are going to pass the new college name to change. You can notice that we are calling the change_college_name method, using the classname. Before and after, we are printing the college name.

You can try executing the program and observing the output. You can find the college name changed. So, as and when we are required to access or change the class state, we can make use of the class methods.

Alternatively, we can directly access the reference variable through the classname and modify the required value. Let’s have a look at a program, which demonstrates the same –

As you can see in the above program, we have many things from the previous program and we are accessing the class variable using the class name, and then changing the value. You can find that again, the value changes.

But, if you try to do the same thing with the object reference variable, you won’t be able to change it. Let’s try to do this as well.

As you can see in the above program, we have many things similar to the previous example, but this time, we are doing s1.college_name, and assigning a value to it. But here, instead of changing the class variable, a new instance variable will be created for the s1 object, and the value would be assigned to it. So, it is not there for everyone, but just for that instance. So, the college name has not changed for everyone.

So, using the class methods, we can access or change the class state. As mentioned earlier, we can also make use of the classmethod() method. Let’s have a look at a program, which tries to demonstrate the same thing.

As you can see in the above program, we have many things, similar to the previous program and as you can see, we have used the classmethod() function, which returns the class method for the given function.

So, this was much about the class methods in python. Basically, the class methods are used to access and change the class state. As and when required, we can make use of the class methods in our programs.