Python private method and private Property property understanding

Source: Internet
Author: User

Private properties, methods--python does not have true privatisation support, but can be used to get pseudo-private
Try to avoid defining variables that start with the underscore
(1) The member variable _xxx "single underline" is called a protection variable, which means that only class objects (that is, class instances) and subclass objects can access these variables themselves, which need to be accessed through the interface provided by the class; cannot be imported with ' from module import * '
(2) The private variable/method name in the __xxx class (a Python function is also an object, so a member method called a member variable can also work.) , the "double underscore" begins with a private member, meaning that only the class object can access it, and even the subclass object cannot access the data.
(3) __xxx__ System definition name, there is a "double underline" for the special method in Python, such as __init__ () represents the constructor of the class.

in [6]:classDog (): ...: def __sit_down (self): ...: Print ('sit down.') ...: def sit_down (self,host_name): ...:ifhost_name=='Master': ....: Self.__sit_down () in [7]: w=Dog () in [8]: W.sit_down ('Master') sat down in [9]: W.sit_down ('Master') in [Ten]: W.__sit_down ()---------------------------------------------------------------------------Attributeerror Traceback (most recent)<ipython-input-Ten-16df8360e9bf>inch<module>()---->1W.__sit_down () Attributeerror:'Dog' Objecthas no attribute'__sit_down'In [ -]: W._dog__sit_down () sit down.

Python instances can invoke the public method of Python directly, and private methods and properties cannot be called directly from a property or method name, and the private methods and properties are added in front of the "_ Class name"

In [ -]: Dir (Dog) out[ -]:                   [' _dog__sit_down ',          '__class__',               '__delattr__',             '__dict__',                '__dir__',                 '__doc__',                 '__eq__',                  '__format__',              '__ge__',                  '__getattribute__',        '__gt__',                  '__hash__',                '__init__',                '__init_subclass__',       '__le__',                  '__lt__',                  '__module__',              '__ne__',                  '__new__',                 '__reduce__',              '__reduce_ex__',           '__repr__',                '__setattr__',             '__sizeof__',              '__str__',                 '__subclasshook__',        '__weakref__',             ' Sit_down ']

Python private method and private Property property understanding

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.