classmethod() in Python

Video Tutorial
FREE
Class Methods thumbnail
This video belongs to
Python Course for Beginners With Certification: Mastering the Essentials
16 modules
Certificate
Topics Covered

Overview

The classmethod() returns a class method for a given function passed. Class methods can be created either with the decorator @classmethod or classmethod() function.

Syntax for Class Method in Python

The classmethod function receives a function as the only argument. The first parameter of the function has to be the class/instance.

We can use the @classmethod decorator to achieve the same results.

Parameters for Class Method in Python

classmethod() accepts a single parameter, function that is to be converted into classmethod.

Return Values for Class Method in Python

classmethod() returns the class method corresponding to the function passed.

Exceptions for Class Method in Python

classmethod() does not raise any exceptions by itself.

What is Class Method in Python ?

Class method is tied to its class rather than the instances created with that class. class methods can access class variables and methods.

Note : Class variables are not to be confused with instance variables.

classmethods can be called from either class itself (or) instance.

Since the class gets implicitly passed to the class method as the first argument, we have to treat the cls as a class instead of an instance.

How to Use Class Method in Python ?

Class methods are especially useful in the case of factory methods. We'll take a look at a specific example below.

Factory method : Factory methods return a new instance/object for several use cases.

Output :

In the above snippet, we're creating two classes, Square and Rectangle. Rectangle has a classmethod which can return a Rectangle instance back with Square's dimensions. Note that, we're calling the classmethod from the "class".

classmethod in Inherited Classes :
Class methods also have another use case. Since, the function has no particular association with the class's origin, the inherited classes, can use the parent's class method as their own, without worrying they would return the parent class's instance. Let's see an example.

Output :

In the above snippet, lay_eggs is defined as a classmethod in the class Bird. This function returns an instance of parent class with age set to 1 and name as the parameter child_name. Note that, even though lay_eggs is defined in the Bird class, it returns the class instance from which it is called from.

More Examples

In the above example, we have created a classmethod with the decorator. It is a factory method that creates a new object from name and birth_date.

Output :

print_occupation is another classmethod created with the build and is just a utility function that accesses class variables. We're printing the occupation attribute of the class (not the instance).

@classmethod Decorator

Decorators are just nested functions, which wrap the original function.

@classmethod is a concise way of creating a class method instead of enclosing the function with classmethod. It's also considered more "pythonic".

Conclusion

  • Class methods in python can be created in two ways, either with the function or decorator.
  • They are primarily used in factory methods. They behave nicely with inherited classes.

Explore Scaler Topics Python Tutorial and enhance your Python skills with Reading Tracks and Challenges.

Further Reading: