The meaning and usage of underscore in Python

        When looking at the code, you often see various variable names with various underlines, such as single underline, double underline, etc. There are five main types of underline (named according to the position of the underline):

  • Single underscore: _
  • Single underscore variable name: _var
  • Variable list underscore: var_
  • Double underscore variable names: __var
  • Double underscore variable name Double underscore: __var__

1. Single-line dash

        Single-line dash "_": Commonly used to indicate temporary variables, irrelevant variables, and a special variable in the Python REPL.

  •        Temporary variable: For example, in a loop, "_" can be used to represent the loop variable, which is only suitable for temporary application.

  •        Irrelevant variables: When splitting a tuple, you can use "_" to represent unnecessary information in the tuple, and you don't need to define additional variable names, as follows:

  •         A special variable in the Python REPL: used to represent the result of the most recent expression by the python interpreter. When you want to view the running result of the previous expression without defining the variable name in advance, you can use "_" to output the previous result, as follows:

        To put it simply, the main purpose of the single-line dash is: for unimportant variables, there is no need to define variable names, so as not to cause confusion in variable names.

2. Single underscore variable names

        Variables or methods starting with a single underscore are for internal use only and cannot be called, as follows:

internal use

        External calls: methods with all names cannot be imported from the module by using wildcard imports, and the error is as follows:

         Variables of type "single-dash variable name" can be imported by using a regular import, as follows:

        Simply put, the single underscore is a Python naming convention that indicates that a name is for internal use . It's usually not enforced by the Python interpreter, just a hint to the programmer.

3. Underscore variable list

        When the variable name already exists or is a Python keyword, if you want to continue using the variable name, add an underscore after the variable name to distinguish it from the already defined variable name or Python keyword to avoid conflicts. As follows: When the variable name "a" has been defined, or when we want to use the python keyword "class" as the variable name, it can be used by adding a single underscore after the variable name.

 4. Double underscore variable names

        The double underscore at the beginning means private , which is only available in the current class, and neither external nor subclasses can call and inherit, but this is not the original intention and purpose of Python's double underscore design. The real purpose of Python's design is to prevent subclasses from overriding their parents . method of the class . As follows: When we want to modify the "__method" function in the parent class in the subclass, it cannot be modified.

 5. Double underscore variable name double underscore

        __var__: Adding an underscore before and after means it is a system-defined name, which is a magic method (see https://cloud.tencent.com/developer/article/1909589 for magic methods ). Ordinary variables cannot be named in this way. __var__ indicates the name of the current module during execution. If the module is executed, the value of __var__ is "__main__". If the module is imported, the value of __var__ is the module name of the imported module.

        __main__: Indicates the entry point where the entire project starts running.

Guess you like

Origin blog.csdn.net/qq_43704127/article/details/128574734