Python: The @classmethod Decorator

Profile picture for user devanshi.srivastava
Submitted by devanshi.srivastava on

The @classmethod decorator, is a built-in function decorator. It is used to declare a method within class method. It is also used to declare a factory method that returns objects of the class.

It can be called by using ClassName.MethodName().

It is an expression that gets evaluated after function is defined. The result of this evaluation shadows function definition. It can return an object of the class. It can access the class attributes but not the instance attributes.


class abc (object):
    def func(cls, arg1, arg2, ...):


class abc:
    def func(cls,a,b):
        print("The answer is:",c)

Output: The answer is: 7