Python the variable name that begins with the underscore meaning

Source: Internet
Author: User

Python core style: Avoid using underscores as the start of variable names.

Because underscores have special meanings for the interpreter and are symbols used by built-in identifiers, we recommend that programmers avoid using underscores as the starting point for variable names. In general, variable name _xxx is considered " private "and cannot be used outside of a module or class. When a variable is private, it is good practice to use _xxx to represent variables. Because the variable name __xxx__ has a special meaning for Python, this naming style should be avoided for common variables.

"single underline "

A member variable that starts with a "single underline" is called a protection variable, meaning that only class objects and self-class objects can access them themselves.

Example: A single underscore (_foo) represents a class attribute that cannot be accessed directly, and must be accessed through the interface provided by the class, and cannot be imported with "from XXX import *" .

" double underline "

A " double underline " begins with a private member, meaning that only the class object can access it, and the child object cannot access the data.

Example: A Double underscore (__foo) represents a private member of a class; a double underscore (__foo__) that begins and ends with a python The special method-specific identifier, such as __init__(), represents the constructor of the class.

Python the variable name that begins with the underscore meaning

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.