Static methods and class methods are created:
Wrap them in the objects of the Staticmethod and classmethod classes, respectively.
There is no parameter self in the definition of a static method, which can be called directly from the class.
The definition of a class method contains a parameter similar to self, which is typically named CLS. For a class method, it can also be called directly through an object, but the parameter CLS is automatically associated to the class.
Example:
1 classMyClass:2 3 defSmeth ():4 Print("This is a static method")5 6Smeth =Staticmethod (Smeth)7 8 defCmeth (CLS):9 Print("This is a class method of", CLS)TenCmeth = Classmethod (Cmeth)
It's a little cumbersome to replace the manual box like this. So introduce adorners (adorners can be used to wrap any callable object, and can be used in methods and functions), you can specify one or more adorners, for which you can list the adorners using the operator @ before the method (or function).
when you specify multiple adorners, the order in which they are applied is reversed (called forward from the back).
1 classMyClass:2 3 @staticmethod4 defSmeth ():5 Print("This is a static method")6 7 @classmethod8 defCmeth (CLS):9 Print("This is a class method of", CLS)
After defining these methods, you can use them as follows (without instantiating the Class):
IS isclass method of <class'__main__. MyClass'>
Static methods and class methods for Python3