Python classmethod function is used to create a class method. It is an inbuilt function that returns the class method for the given function. It takes a single parameter.
A class method can be called both by a class and its object. Read more…
Python classmethod function exmaple
Simple example code Create a class method using classmethod().
class App: price = 120 def display(self): print('The price is:', self.price) # create display class method App.printPrice = classmethod(App.display) App().display()
Uses classmethod in Python
- Generally it used to create factory methods. Factory methods return the class object ( similar to a constructor ) for different use cases.
- Another uses static methods to create utility functions.
Do comment if you have any doubts and suggestions on this Python function topic.
Note: IDE: PyCharm 2021.1.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.