The difference between a single underline and a double underline in Python (private and protected)

Source: Internet
Author: User

PYTHON specifies special variables with underscores as variable prefixes and suffixes.

_xxx cannot be imported with ' from Moduleimport * '
__xxx__ System Definition Name
Private variable names in the __xxx class

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.

A member variable that starts with a "single underline" is called a protection variable, meaning that only class objects and subclass objects can access these variables themselves;
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.

A class attribute that begins with a single underscore (_foo) cannot be accessed directly, and is accessed through the interface provided by the class, cannot be imported with a "from XXX import *", and a double underscore (__foo) represents a private member of the class; A double underscore that starts and ends (__foo__ Represents a special method-specific identifier for Python, such as __init__ (), which represents the constructor of a class.

Conclusion:

1. _xxx cannot be used for ' from module import * ', which begins with a single underscore, is a variable of type protected. That is, the protection type can only allow access to its own and subclasses.

2, __xxx double underline is a variable of the private type. Only allow access to the class itself. You can't even subclass it.

3. __xxx___ defines the method of the special column. Like __init__ or something.

The difference between a single underline and a double underline in Python (private and protected)

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.