What is the Usage of @staticmethod and@classmethod in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

What is the Usage of @staticmethod and@classmethod in python

What is the Usage of @staticmethod and@classmethod?

28th Jul 2017, 1:54 AM
jonathan0qsq
jonathan0qsq - avatar
2 Answers
+ 4
Both are decorators that allow you to create methods in your class to perform very general functions. None of them has access to the attributes or methods of the class. So the data must be sent by parameters, in the case of @classmethod has access to create an instance of the class using the word cls to invoke the constructor of the class.
29th Jul 2017, 4:01 PM
Javier I. Rivera R.
Javier I. Rivera R. - avatar
+ 3
@staticmethod can be invoked from the methods of a class with self or the name of the class, even from the instances. nameclass.st_method(n) or selft.st_method(n) @classmethod are only executed from the class mainly to create instantiations obj = nameclass.cl_method(n)
29th Jul 2017, 7:02 PM
El CHR of MRD
El CHR of MRD - avatar