python-Pseudo-Private properties

Source: Internet
Author: User

Original: http://blog.itpub.net/26250550/viewspace-1411768/

Usually in Python, we are told that a method name can be used to define a method that starts with a double underscore to reach the target of a private function.
In fact, this understanding is wrong, this method is more a strong hard to put together the customary method.

First of all, in Python, there is no concept of access control, this is different from other OO languages, Python philosophy is assumed that the user will use, do not require the designer access rights, this is in many parts of Python design ideas are consistent, is to be simple, to believe that everyone is thinking of people.

So what is the double underline? In fact, in Python, the class method name, which begins with a double-dip, is automatically called after the class instantiation and _classname in front of its name, because the name is changed, so nature cannot be accessed with the name of the double-dip-start, thus achieving the non-entry purpose.

Other languages, such as Java,c++ 's OOP syntax are compared to the specifications, there are public, private and protected data types, and Python, from my current observation, Python class is not permission control, all variables can be called externally, then you will say: " Python clearly has a private definition method is to add a double-dip line in front of the variable or method, but I tell you, this is actually Python's pseudo-private. It's just a programmer's agreement that the rules are called private variables, but you can call them if you want to call them externally.

Private variable: instance. _ Class name __ variable name
Private method: Instance. _ Class Name __ Method name ()
For example:

classInfo:def __init__(self): self.__name='Jay'    def __say(self):Print(self.)__name)#access Method:A =Info ()Print(A._info__name)#' Jay 'Print(A._info__say ())#' Jay '
## # Get Properties and methods for a class#get all properties of a class>>>Print(A.__dict__){'_info__name':'Jay'}#get all properties and methods of a class>>>Print(dir (a)) ['_info__name','_info__say','__class__','__delattr__','__dict__','__dir__', '__doc__','__eq__','__format__','__ge__','__getattribute__','__gt__','__hAsh__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

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.

The beginning of the original is to represent private, private is not inheritable

Python has a private definition method that adds a double-dip line to the front of a variable or method, but in fact it is a pseudo-private python. It's just a programmer's contract. The rules are called private variables, but if you want to call them externally, you can call them as follows:
All variables starting with double underscore are translated by Python with single underscore and class name
As the attribute __inaccessible translates to secretive._secretive__inaccessible, note that the first one is a single underline and the second is a double underscore:

>>>classSecretive:def __inaccessible(self):#double underline means private method        Print("Bet You can ' t see me ...")      defaccessible (self):Print("The secret message is:") self.__inaccessible()              >>> s =secretive ()>>> S.__inaccessible()#private methods are inaccessible from outside, errorTraceback (most recent): File"", Line 1,inchS.__inaccessible()#private methods are inaccessible from the outside worldAttributeerror:secretive instance has no attribute'__inaccessible'  
>>>
>>>>>> s.accessible ()#private methods can only be used inside a classThe secret message is: Bet You can't See me ...
>>>
>>>>>> s._secretive__inaccessible ()#Although private, can still be called, pseudo-private mechanismBet You can't See me ...

python-Pseudo-Private properties

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.