This article mainly introduces the underline in Python, this article explains the single underline directly make variable name, single underline prefix name, double underline prefix name and so on, need friends can refer to the following
This article discusses the use of the underscore _ in Python. Like many uses in Python, the majority of the different uses of underline _ (not all of them) is a convention convention.
One, single underline directly do variable name (_)
There are three main types of cases:
1. In the Interpreter
The _ symbol refers to the return result of the last execution of the statement in the interactive interpreter. This usage was initially found in the CPython interpreter, and other interpreters were followed up later.
The code is as follows:
>>> _
Traceback (most recent call last):
File "", Line 1, in
Nameerror:name ' _ ' is not defined
>>> 42
>>> _
>>> ' alright! ' if _ Else ':('
' Alright! '
>>> _
' Alright! '
2. Use as a name
This is a bit like the above. _ As the name to be discarded. By convention, doing so allows the person who reads your code to know that this is a specific name that will not be used. For example, you may not care about the value of a loop count:
The code is as follows:
n = 42
For _ in range (n):
Do_something ()
3. i18n
_ can also be used as a function name. In this case, a single underline is often used as a function name for internationalized and localized string translation queries. This practice seems to originate from the C language. For example, in the Django documentation for translation you might see:
The code is as follows:
From django.utils.translation import Ugettext as _
From django.http import HttpResponse
def my_view (Request):
Output = _ ("Welcome to My Site.")
return HttpResponse (Output)
The second and third usages can cause conflicts, so if you use _ as the i18n translation query function in any block of code, you should avoid being used as a discarded variable name.
The name of the single underline prefix (for example, _shahriar)
A name prefixed with a single underline specifies that the name is "private". In some scenarios that import imports *, the next person to use your code (or yourself) will understand that the name is used internally only. Python documentation wrote:
A name prefixed with a underscore (e.g. _spam) should to treated as a non-public part of the API (whether it is a functio N, a method or a data member. It should be considered a implementation detail and subject to change without.
The reason for this is that in some import * scenarios, the interpreter does handle the name at the beginning of the single underscore. If you write the from import *, any names that start with a single underline will not be imported unless the module/package __all__ list explicitly contains the names. See "Importing * in Python" for more information.
Three, the name of the double underscore prefix (for example, __shahriar)
A name that is prefixed with a double underline (especially the method name) is not a convention; it has a specific meaning to the interpreter. Python overwrites these names to avoid conflicts with names defined in subclasses. As the Python documentation mentions, any __spam in this form (at least at the beginning of a two underscore, most of which also has an underscore ending), is replaced with _classname__spam on the text. Where classname is the current class name and is prefixed with an underscore.
Look at the following example:
The code is as follows:
>>> class A (object):
... def _internal_use (self):
. Pass
... def __method_name (self):
. Pass
...
>>> dir (A ())
[' _a__method_name ', ..., ' _internal_use ']
As expected, _internal_use did not change, but __method_name was rewritten as _classname__method_name. Now to create a subclass B (which is not a good name), you won't easily overwrite the __method_name in a:
The code is as follows:
>>> class C (object):
... def __mine__ (self):
. Pass
...
>>> dir (C)
... [..., ' __mine__ ', ...]
Or don't write the method name like this, just let the special method name defined by Python use this convention.