The difference between an object-named single underline and a double underline in Python (private and protected)

Source: Internet
Author: User

in Python, a single double underline is used as a variable prefix and suffix to specify the meaning of a particular variable :

_name cannot be imported with ' from Moduleimport * '
__name__ System Definition Name
Private variable names in the __xxx class because underscores have special meaning to the interpreter, and the symbolic variable name used by the built-in identifier _xxx is considered "private" and is not available outside the module or class. It's a good habit to use _xxx to represent variables when the variables are private. Because the variable name XXX has a special meaning for python, you should avoid this naming style for ordinary variables. The member variable that begins with a single underline is called a protection variable, meaning that only class objects and subclass objects can access these variables. A "double underline" is a private member, meaning that only the class object can access it, and even the subclass object cannot access the data. A class attribute that is not directly accessible by the representative of the beginning of a single underline (_FUNC). Access is required through the interfaces provided by the class, and cannot be imported with the "from name import *"; (func) that begins with a double underscorerepresents a private member of the class; a double underline beginning and ending (__foo represents a particular method-specific identifier in Python, such as init(),del(), and new , which represents the constructor of the class.

1. _name cannot be used for ' from module import ' with a single underline that represents a variable of type protected. That is, the protection type can only allow access to itself with subclasses.

2, the __name double underline is a variable of the private type. Can only be allowed to access the class itself. It is not possible to even subclass.

3,name_ definition is the Special column method. Like init,del or something.

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.