+ 6
What is the Usage of @staticmethod and@classmethod in python
What is the Usage of @staticmethod and@classmethod?
2 Respostas
+ 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.
+ 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)