Python learns the calling rules of classes and methods

1 The characteristic of a class method is that the class method does not belong to any object of the class, but only belongs to the class itself

The static method of the 2 class is similar to the global function, because the static method has neither the self parameter of the instance method nor the cls parameter of the class method, and anyone can call it

3. Instance methods only belong to instances, and can only be called by instantiated objects

 

 

 

1. If you need to use an instance to call your method, then when defining the method, be sure to set the first parameter to self;
2. If you need to use a static method, then you need to add @staticmethod in front of the method Modifier;
3. If you want to use a class method, you need to add the @classmethod modifier in front of the method, and use at least one parameter in the method. The function of the first parameter in the method is to represent the class itself.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325158865&siteId=291194637