Page Contents
In this tutorial we will learn about Class method in Python class . Before reading further we recommend to read about the below tutorial –
Class method in Python
The class method is the behavior of the class which can be accessed by any object of that class besides the class.
Syntax of Class method in Python
@classmethod
def method_name(cls,parameters):
statements
Before defining any class method we have to use @classmethod otherwise the method will be treated as object method. Some similarities between object method and class method are given below in the form of list –
- Just as the first parameter is always treated as a reference to an object when defining an object method, so the first parameter is always treated as a reference to a class when defining a class method.
- Just as we can use any other word in place of
self
in the first parameter during object method definition, we can use any other word in place ofcls
in the first parameter in class method definition. Butself
,cls
are programming convention and we will always usecls
in the first parameter of class method.
Note :
We have known previously that class variables can be modified only with class_name but using class method we can also change class variables with object_name.
Calling a Class method in Python
class_name/object_name.(parameters)
Examples of Class method in Python
Example 1:
#creating a class
class Myclass:
#creating a class variable
total_student = 15
#creating a object method
def set_number_of_students (self, no_of_students):
#creating a object variable same
#name with class variable
self.total_student = no_of_students
#creating a class method
@classmethod
def set_total_student (cls, no_of_students):
#modify the value of class variable
cls.total_student = no_of_students
In the above code, a class named Myclass
has been written which has a class variable named total_student
whose value is 15. Then an object method called set_number_of_students
is written which takes no_of_students
as an argument and creates a new object variable total_student
whose value is equal to no_of_students
. Then a class method set_total_student
is defined which changes the value of class variable total_student
to equal the value of no_of_students
given in the argument.
#creating a object
sourav = Myclass()
#accessing class variable
print(Myclass.total_student)
15
Then an object named sourav
is created and the class variable total_student
is printed which prints 15
.
#creating a object variable same name
# with class variable using object method
sourav.set_number_of_students(20)
#accesing object variable
print(sourav.total_student)
20
Then the object method set_number_of_students
is called with argument as 20 which makes an object variable total_student
whose value is 20
, so when we print sourav.total_student
it prints 20
.
#changing the value of class variable using
#class method accessing by class name
Myclass.set_total_student(25)
print(Myclass.total_student)
25
Then we call the class method set_total_student
with argument as 25
through class_name . So the value of the class variable total_student
has changed to 25
, so when we printed Myclass.total_student
it printed 25
.
Change the value of class variable using object name
#changing the value of class variable using
#class method accessing by object name
sourav.set_total_student(30)
print(Myclass.total_student)
print(sourav.total_student)
30
20
Then we call the class method set_total_student
with argument as 30
through object_name so that the value of the class variable total_student
has changed to 30
, so when we printed Myclass.total_student
it printed 30. But the object variable did not change so it printed 20
.
Note :
We can use a class method as an alternative constructor.
Example 1: Use class method as an alternative constructor
#creating a class
class Myclass:
#creating a constructor
def __init__ (self, name, age):
#creating object variable
self.name = name
self.age = age
#creating a class method
@classmethod
def alternative_constructor (cls, info):
return cls(*info.split())
In the above code we have written a class. In that class we have defined a class method which takes only one parameter except self. We converted the value of that parameter into a list using split()
method. Then we have unpacked the list with asterisk (*) and returned it with class.
#creating a object
sudipta = Myclass("sudipta", 26)
#accessing object variable
print(sudipta.name)
print(sudipta.age)
sudipta
26
Then we have created an object named sudipta
and printed its object variables.
#creating a object using class method
sourav = Myclass.alternative_constructor("sourav 28")
#accesing object variable
print(sourav.name)
print(sourav.age)
sourav
28
Now when class method is called while creating sourav
object, "sourav 28"
is first converted to ["sourav", 28]
by split()
method, then it is changed to "sourav"
, 28
by asterisk(*
) operator. That is, Myclass.alternative_constructor("sourav 28")
is treated as Myclass("sourav", 28)
.Thus an object is created through class method . So then when the object variables of the sourav
object are printed, they print "sourav"
and 28
respectively.
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .