Meaning of the Python underline variable

Source: Internet
Author: User

ZZ from http://blog.163.com/yang_jianli/blog/static/1619900062011627103516435/

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

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

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, 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. Weak internal variable markers, such as when using "from M import", do not introduce an object that begins with an underscore.

2, __xxx double underline is a variable of the private type. Can only be allowed to access the class itself, and even subclasses can not be used to name a class attribute (class variable), called when the name is changed (inside the class FooBar, __boo into _foobar__boo , such as Self._foobar_ _boo )

3. __xxx___ defines the method of the special column. Variables or attributes within a user-controlled namespace, such as __init__,__import__ or __file__. Do not define such variables yourself unless the document is described. (that is, these are the variable names defined within Python)

Meaning of the Python underline variable

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.