Introduction to classmethod() in Python
To use the Class Method in Python, We have to use the Class method decorator ‘@classmethod’. The class method decorator is defined just before the function definition. Class is passed in the function as a parameter, and it should be the first parameter. We use the ‘cls’ keyword for passing to the function. Cls denotes that class is passed as a variable. This is done so that we can modify the variable of the class and objects. As everything is an object in Python.
def methodName(cls, arg1,arg2 ….):
(lines of code)
How classmethod() Works in Python?
The class method has only access to parameters of the class and arguments. The class method doesn’t have self arguments, so it can have access to only the class itself and the object representing the class. When we create an instance of the class and call the class method, we won’t be able to access the self-object; it can only have access to the attributes of the class itself.
When we want to work on factory methods, class methods are recommended as it returns the class object. Class method decorator allows us to call functions within classes without having to instantiate them. While using the class method, please note that it receives the lowest class of the calling subject, and it might result in implications when trying to update the data of the class through the passed in class.
We can that class method always implement the lowest in the instance tree. Using a class method is best suited when are processing data that might be different for each class in the hierarchy.
Examples to Implement classmethod in Python
Below are the examples of classmethod in Python:
def __init__(self, name):
self.studentname = name
def squareroot(cls, number):
sampleclass.result = number * number
The above program calculates the square root of the number. We have created the class ‘sampleclass’ and defined a constructor inside it. We have defined the class method decorator before the method. We have created a method ‘squareroot’, and the first parameter is the cls, which denotes class and passed number parameter. We have also created a class-level variable ‘result’ that is storing the output.
Now you can call the method using classname.methodname and pass the parameter.
def __init__(self, features):
self.features = features
return cls(['ABS', 'Disk Brakes'])
return cls(['ABS', 'Disk Brakes', 'GPS', 'Alloy Wheels'])
In the above program, we have created a class ‘Car’, and we have also defined a constructor. We have defined two-class method ‘audi’ and ‘mercedes’, and we have created instances of the class. These values will be returned when we will call our class method. We can call our Audi and Mercedes class by just calling Car class; we don’t need to pass any instance.
intern = 0
increment = 10000
def __init__(self, firstname, lastname, salary):
self.firstname = firstname
self.lastname = lastname
self.salary = salary
def set_increment(cls, amount):
cls.increment = amount
emp_1 = Organisation('Corey', 'Schafer', 50000)
emp_2 = Organisation('Test', 'Organisation', 60000)
In the above program, we have created a class Organisation and created two variables, intern and increment. We have defined a self constructor that is taking 3 parameters. We have defined our class method, created class variable increment, and assigned the amount argument that we have passed along with cls.
We have created two organization instances. Now we comment out ‘Organisation.set_increment(15000)’ the line, then we get the output as 10000 for all three print commands as we have already assigned 10,000 at the class level. Now we call Organisation class and call our class method, i.e. set_increment. So we are going the amount in the class method, and now if we run it, all of the output will be 15000. It happened because we ran the set_increment method, i.e. class method, which means now we are working with class instead of instance, and we are setting that class variable raise amount equal to the amount that we have passed here.
We can also run a class method using instance also, but that doesn’t make any sense, and it’s not a good practice. We can use the class method as an alternative constructor and provide multiple ways of creating objects.
The class method is very useful for creating code, and managing code is very easy; we don’t need to make a change everywhere in the code. Creating an API with a classmethod is very easy. The Best use of the class method is when we are making too many methods, and we want to call those methods using class, not objects.
This is a guide to classmethod in Python. Here we discuss a brief overview on classmethod in Python and its Examples, along with its Code Implementation. You can also go through our other suggested articles to learn more –