Single underline
The single underscore is used as a variable
- One of the most common use scenarios is as variable placeholders, which can obviously reduce the use of redundant variables in your code. And just to make it easier to understand,
_
Think of it as a discarded variable name. This lets people reading your code know that this is a variable that will not be used, e.g.
for _, _, filenames in os.walk(targetDir):
print(filenames)
for _ in range(100):
print('PythonPoint')
Copy the code
- In interactive interpreters like iPython,
_
The variable points to the return of the last executed statement in the interactive interpreter.
Single-underscore prefix names (for example _pythonPoint)
-
This means that it is a protected member (property or method) that only class objects and subclass objects themselves can access, and is a way to specify private variables and methods (just by convention). If you import from a_module import *, these variables and functions will not be imported. It is worth noting, however, that if you import modules as import A_module, you can still access such objects in the form a_module._pythonPoint.
-
There is another case where single-underscore leads are not commonly used. For example, an extension library written in C will sometimes be named with underscores and then wrapped in a Python module that removes the underscores. A module such as struct is actually a Python wrapper around the C module _struct.
Single-underscore suffix name
Usually used to distinguish it from a Python keyword. For example, we need a variable called class, but class is a Python keyword and can be written class_ with a single underscore.
Double underline
Double underscore prefix name
This indicates that this is a private member (property or method). It can’t be accessed directly like a public member. The double-underscore form of naming is used in Python class members to represent names, that is, if the Test class has a member __x, then dir(Test) will see _Test__x instead of __x. This is to avoid conflicts between the name of the member and the name of the subclass, and facilitate the identification of the member in the parent class and the subclass. Note that this requires at most one underscore at the end of the name. e.g.
Double underscore prefix and suffix names
A convention, Python internal names, used to distinguish other user-defined names in case of conflicts. Are some Python “magic” objects that indicate that this is a special member. Such as class members __init__, __del__, __add__, etc., as well as the global __file__, __name__, etc. Python’s official recommendation is never to use such naming for your own variables or functions, but instead to follow the documentation for using Python’s built-in special members.
Python’s official documentation on private attributes and method conventions is as follows
“Private” instance variables that cannot be email exchange except from inside an object don’t exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice.
Since there is a valid use-case for class-private members (namely to avoid name clashes of names with names defined by subclasses), there is limited support for such a mechanism, called name mangling. Any identifier of the form__spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped. This mangling is done without regard to the syntactic position of the identifier, as long as it occurs within the definition of a class.
Name mangling is helpful for letting subclasses override methods without breaking intraclass method calls.
The above –