In Python, OOP is used to implement real-world entities such as inheritance, polymorphisms, encapsulation, etc. We can use OOP to define the “instance” and “class” methods. The instance method is utilized to access or change the object’s state, while on the other side, the class method is utilized to access or modify the class state.
This Python post will deliver an in-depth guide on Python class methods using numerous examples. This Python tutorial will discuss the following contents:
- What is the Python Class Method?
- How to Define Class Method in Python?
- How to Use Class Method in Inheritance in Python?
What is the Python Class Method?
A “class method” in Python is associated with the class instead of any particular object instance. This method can be invoked on a class directly instead of the class instance. All class instances can share the class method because it is associated with a class level.
How to Define Class Method in Python?
To define the class method, the “classmethod()” function and “@classmethod” decorator are used in Python.
Method 1: Using classmethod() Function
In the below code, the “classmethod()” function is used to convert the defined normal method into class method:
class company: name = 'XYZ' def cname(cls): print('Company Name is :', cls.name) company.cname = classmethod(company.cname) company.cname()
- The class named “company” is defined in the program.
- The “name” variable is defined inside the class with the value “XYZ”.
- The method of the class named “cname()” is defined in the program. This method takes the class argument “cls” and shows the company name stored in a variable “name”.
- The expression “classmethod(company.cname)” converts the method “cname” into a class method.
- Finally, the expression “company.cname()” calls the class method “cname” without creating an instance of the class.
The class method of the given function has been returned on screen.
Method 2: Using @classmethod Decorator
In the below code, we define a class students which contains the “__init__()” constructor and a class method that is defined using the “@classmethod” decorator:
class students: def __init__(self, name, age): self.name = name self.age = age @classmethod def team(cls, value): print(value*value) team = students.team(5)
- The class named “students” is defined in the program.
- The “__init__()” method is defined in the program and automatically called when an object of the class is defined. It has three arguments: name, self, and age.
- Next, we use the “@classmethod” decorator to define a class method named “team”.
- The expression “students.team(5)” creates an object of the student’s class and accesses its class method “team” by passing the argument “5”.
The class method “team” value has been displayed.
How to Use Class Method in Inheritance in Python?
The class method can also be used in the child class because the parent class method is accessible to a child class. The below code clarifies this concept:
class Students: name = "Joseph" @classmethod def get_name(cls): return cls.name class junior(Students): name1 = "Henry" Junior = junior() print(Junior.get_name())
- A base class “Students” and a derived class “junior” is defined in the program.
- The “attribute” name is defined in the class with the string value “Joseph”.
- We define a class method named “get_name()” using “@classmethod decorator”.
- The subclass or child class named “junior” is defined in the program with the attribute “name1”.
- We created the object for the junior class and assigned it to the ” Junior ” variable.
- The get_name method is called on the Junior object, which returns the value of the “name” attribute, “Joseph”.
The output of the “get_name” method, “Joseph“, is printed to the console.
A “class method” in Python is associated with the class instead of a particular initial class instance. The “classmethod()” function and “@classmethod” decorator are utilized to create/define the class method. The “classmethod()” converts the normal method to the class method, and the “@classmethod” decorator is used to define/create the class method. This guide delivered an in-depth overview of Python class methods using various examples.