Python classmethod function | Use with example

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)



Python classmethod function

Uses classmethod in Python

  1. Generally it used to create factory methods. Factory methods return the class object ( similar to a constructor ) for different use cases.
  2. 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)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.