Python Tutorial 39 --- "Class Methods in Python(OOPS 4)"
Python Tutorial 39 --- "Class Methods in Python(OOPS 4)"
As we have observed in the previous tutorials, we cannot change the value of a variable defined in the class from outside, using an object. Instead, if we try that, a new instance variable will be created for the class having the value we assigned. But no change will occur in the original value of the variable.
We saw the use of self keyword in the previous tutorial. In this tutorial, we are going to know the working of a new keyword i.e., cls. Class methods take cls parameter that points to the class and not the object instance when the method is called.
Syntax:
myfunc defines the function that needs to be converted into a class method
returns: @classmethod returns a class method for function.
Because the class method only has access to cls argument, it cannot modify object instance state. However, class methods can still modify class state that applies to all instances of the class. So a class method automatically recognizes a class, so the only parameter that remained to be passed is the function that needs conversion.
@classmethod Decorator:
We have covered decorators in detail in Tutorial #51, so here we are just doing to define the functionality of decorator instead of its working. A @classmethod Decorator is a built-in function in Python. It can be applied to any method of the class. We can change the value of variables using this method too.
Differences between Class Method and Static Method:
Class method | Static Method |
Taking a class or, in short, form cls as an argument is a must for a class method. | There is no such restriction of any specific parameter related to class in the Static method. |
With the help of class methods, we can change and alter the variables of the class. | With a static method, we can not change or alter the class state. |
Class methods are restricted to OOPs, so we can only use them if a class exists | The static method is not restricted to a class |
We generally use class methods to create factory methods. Factory methods return a class object which is similar to a constructor for different use cases. | Static methods are used to create utility functions. |
A quick overview:
Python class method is a way to define a function for the Python class. It receives the class as an implicit first argument. Using @classmethod decorator, it is possible to create as many constructors for a class that behaves like a factory constructor.
Code ---
# Class Methods In Python
class employee:
no_of_leaves = 8
def __init__(self, aname, asalary, arole):
self.name = aname
self.salary = asalary
self.role = arole
def printdetails(self): # self is a method
return f"Name - {self.name}, Salary - {self.salary}, Role - {self.role}"
@classmethod
def change_leaves(cls, newleaves):
cls.no_of_leaves = newleaves
saswata = employee("Saswata", 500, "Freelancer")
tanmoy = employee("Tanmoy", 1000, "Chopwala")
ritam = employee("Ritam", 769, "Netangineer")
saswata.no_of_leaves = 50
print(saswata.no_of_leaves)
Comments
Post a Comment