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