Questions And Answers

More Tutorials

Python Class Methods

Class methods take cls parameter that points to the class and not the object instance when the method is called.

class myClass:
    def myfunc (cls, arg1, arg2, ...):

myfunc defines the function that needs to be converted into a class method

returns: @classmethod returns a class method for function.

Because the class method only has access to the cls argument, it cannot modify the object instance state. However, class methods can still modify the class state that applies to all instances of the class. So a class method automatically recognizes a class, so the only parameter that remains to be passed is the function that needs conversion.

@classmethod Decorator:

A @classmethod Decorator is a built-in function in Python. It can be applied to any method of the class. We can change the value of variables using this method too.

Differences between Class Method and Static Method:

Class method

Taking a class or, in short, form cls as an argument is a must for a class method.
With the help of class methods, we can change and alter the variables of the class.
Class methods are restricted to OOPs, so we can only use them if a class exists.
We generally use class methods to create factory methods. Factory methods return a class object which is similar to a constructor for different use cases.

Static Method

There is no such restriction of any specific parameter related to class in the Static method.
With a static method, we can not change or alter the class state.
The static method is not restricted to a class.
Static methods are used to create utility functions.

The Python class method is a way to define a function for the Python class. It receives the class as an implicit first argument. Using @classmethod decorator, creating as many constructors for a class that behaves like a factory constructor is possible. Hopefully, now you can apply this concept to your projects and use it to improve your code's organization and quality.


In this page (written and validated by ) you learned about Python Class Methods . What's Next? If you are interested in completing Python tutorial, your next topic will be learning about: Python Class Methods As Alternative Constructors.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.