Underline details in Python-Python tutorial

Source: Internet
Author: User
Tags i18n
This article mainly introduces the underline details in Python. This article explains how to use a single underline for variable name, single underline prefix name, double underline prefix name, and so on, you can refer to this article to discuss the use of underline _ in Python. Similar to many usage in Python, most (not all) different use of underscore _ is a convention.

1. a single underline is directly used as the variable name (_)

There are three main cases:

1. in The Interpreter

_ Symbol refers to the return result of the last statement executed in the interactive interpreter. This method was initially used 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 the name

This is a bit similar to above. _ Used as the discarded name. By convention, this will let people who read your code know that this is a specific name that will not be used. For example, you may not have a cyclic count value:

The code is as follows:


N = 42
For _ in range (n ):
Do_something ()

3. i18n

_ Can also be used as the function name. In this case, a single underline is often used as the function name for International and localized string translation queries. This convention seems to have originated from the C language. For example, in Django documentation for translation, you may see:

The code is as follows:


From django. utils. translation import ugettext _
From django. http import HttpResponse

Def my_view (request ):
Output = _ ("Welcome to my site .")
Return HttpResponse (output)


The second and third methods may cause conflicts. Therefore, if _ is used as the i18n translation query function in any code block, it should be avoided to use it as the discarded variable name.

2. name of a single underline prefix (for example, _ shahriar)
The name prefixed with a single underline specifies that the name is "private ". In some import * scenarios, the next user using your code (or yourself) will understand that this name is only used internally. Python documentation writes:

A name prefixed with an underscore (e.g. _ spam) shoshould be treated as a non-public part of the API (whether it is a function, a method or a data member ). it shoshould be considered an implementation detail and subject to change without notice.

In some import * scenarios, the interpreter indeed processes the names starting with a single underline during import. If you write from Import *, any names starting with a single underline will not be imported, unless the _ all _ list of modules/packages explicitly contains these names. For more information, see "Importing * in Python ".

3. names with double-underline prefixes (for example, _ shahriar)

A name prefix (especially a method name) with double underscores is not a convention; it has a specific meaning for the interpreter. Python will rewrite these names to avoid conflicts with the names defined in the subclass. As mentioned in Python documentation, the identifier of any _ spam format (starting with at least two underscores and ending with an underscore in most cases, both texts are replaced with _ classname _ spam, where classname is the current class name and prefix with an underscore.
Let's look at the example below:

The code is as follows:


>>> Class A (object ):
... Def _ internal_use (self ):
... Pass
... Def _ method_name (self ):
... Pass
...
>>> Dir (())
['_ A _ method_name',..., '_ internal_use']

As expected, _ internal_use remains unchanged, but _ method_name is changed to _ ClassName _ method_name. Create A subclass B of A (this is not A good name) and it will not overwrite _ method_name in A easily:

The code is as follows:


>>> Class C (object ):
... Def _ mine _ (self ):
... Pass
...
>>> Dir (C)
... [..., '_ Mine _',...]


Do not write the method name like this. use this convention only for the special method names defined by Python.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.