The underline of the Python

Source: Internet
Author: User


The first is a single underscore, which is often used in modules, variables and functions that begin with a single underscore in a module are treated as intrinsic functions by default, and if imported from a_module import *, this part of the variables and functions are not imported. It is worth noting, however, that if you import a module using Import A_module, you can still access such an object in the form of A_module._some_var.

In Python's official code style, there is also a single underline-ending style, which does not have a particular meaning when parsing, but is often used to differentiate from Python keywords, such as if we need a variable called class, but class is a python keyword, You can write Class_ at the end of a single underline.

A naming form that begins with a double underline is used in a Python class member to represent the name adaptation (name mangling), which means that if there is a member __x in a test class, then Dir (test) sees _test__x instead of __x. This is to avoid a conflict between the name of the member and the name in the subclass. Note, however, that this requires no underscore at the end of the name.

Double underscore the end of the double underline is some Python "magic" objects, such as the __init__ of class members, __del__, __add__, __getitem__, etc., as well as the global __file__, __name__, and so on. The official Python recommendation never apply such a naming method to your own variables or functions, but use them as documented instructions.

    • __double_leading_underscore (Double underline): The class private name from Python 1.4.

    • __double_leading_and_trailing_underscore__: A Special (Magic) object or attribute that exists in the user-controlled (user-controlled) namespace, for example:__ init__, __import__ or __file__. Sometimes they are defined by the user to trigger a particular behavior (magic behavior) (for example: operator overloading); Sometimes the constructor (infrastructure) is inserted so that it can be used by itself or in order to debug. Therefore, in future versions, constructors (loosely defined as Python interpreter and standard library) may intend to create their own list of magical attributes, and user code should generally limit the use of such conventions as their own. User code that wants to be part of the constructor can use a short prefix in the glide line, for example. __bobo_magic_attr__.

In addition, a single underline at the beginning of the general situation is not used in the case of an extension library written with a C is sometimes named after an underscore, and then use a stripped-down Python module for packaging. The module, such as struct, is actually a Python wrapper for the C module _STRUCT.

For more information about naming, refer to the PEP 8 Code style guide, Name Conventions section:http://www. Python.org/dev/peps/pep-0008/

It should be noted that since this document appears to have been modified several times, the use of the beginning of the double underscore seems to have been suggested as a private member of the class, at least THE PEP 8 translation of the Woodpecker community, which is not reflected in the current official version of Pep 8, as @makestory said.

The Woodpecker's translation reference:http/wiki.woodpecker.org.cn/moin/pythoncodingrule

The underline of the Python

Related Article

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.