Python Object Oriented Programming | Class Methods

Class methods are called by a class, which is passed to the cls parameter of the method.

A common use of these are factory methods, which instantiate an instance of a class, using different parameters than those usually passed to the class constructor.
Class methods are marked with a classmethod decorator.


class Rectangle:
	def __init__(self, width, height):
		self.width = width
		self.height = height

	def calculate_area(self):
		return self.width * self.height

	def new_square(cls, side_length):
		return cls(side_length, side_length)

square = Rectangle.new_square(5)

The terminal output will be:

ddn_ro@linux:~/Desktop$ python

The new_square is a class method and is called on the class, rather than on an instance of the class. It returns a new object of the class cls.

Technically, the parameters self and cls are just conventions; they could be changed to anything else. However, they are universally followed, so it is wise to stick to using them.

Leave a Reply