Python instance methods, class methods, static methods

Examples of methods

    Definition: The first parameter is an instance of an object, the parameter name is generally agreed as "self", through which pass properties and methods of instances (you may also transmit properties and methods and the like);

    Call: can only be called by an instance of an object.

Class Methods

    Definition: the decorator @classmethod. The first argument must be a class object is the current, this parameter is generally agreed to name "cls", through which to pass the properties and methods (not pass instance attributes and methods) of the class;

    Call: the class object or instance of an object can be called.

Static method

    Definition: the decorator @staticmethod. Random parameters, no "self" and "cls" parameter, but the method can not be used in the body of any class or instance attributes and methods;

    Call: the class object or instance of an object can be called.

Private methods

  Define private variables only need to add the variable name or function name in python "__" two underscores, then this will be a function or variable to a private

  This method is a method declared private, external calls can not be in class

    Definition: The first parameter is an instance of an object, the parameter name is generally agreed as "self", through which pass properties and methods of instances (you may also transmit properties and methods and the like);

    Call: can only be called by an instance of an object.

Class Methods

    Definition: the decorator @classmethod. The first argument must be a class object is the current, this parameter is generally agreed to name "cls", through which to pass the properties and methods (not pass instance attributes and methods) of the class;

    Call: the class object or instance of an object can be called.

Static method

    Definition: the decorator @staticmethod. Random parameters, no "self" and "cls" parameter, but the method can not be used in the body of any class or instance attributes and methods;

    Call: the class object or instance of an object can be called.

Private methods

  Define private variables only need to add the variable name or function name in python "__" two underscores, then this will be a function or variable to a private

  This method is a method declared private, external calls can not be in class

Guess you like

Origin www.cnblogs.com/tangqiu/p/12595143.html