Private properties and methods for such

What kind of property have access (also called visibility)? In many object-oriented programming language, you will usually attribute set to private (private) or protected (protected), are not allowed access to the outside world, and method They are usually open (public). In Python, access the properties and methods of only two: public and private. If you want to be private, when naming can use two underscore beginning.
However, Python does not guarantee strict privacy private property or method from the grammar , it just gives private properties and methods of a change of name to "hinder" access to them, in fact, if you know the rules change the name can still access to them. The reason for this setting, can be explained by such a famous saying, that "We are all consenting adults here" . Because most programmers think better open than closed, and programmers responsible for their own behavior.
In the actual development, we do not recommend the property to private, because it would lead to a subclass can not access. So most Python programmers will follow a naming convention is to allow a single attribute names begin with an underscore to represent the property is protected , this code outside of class during a visit to the property that should remain cautious. This practice is not grammatical rules, a single underscore the properties and methods of the outside world is still accessible, so the more often it is a hint or metaphor .
 
 
Class of private property
__privateattrs: two begins with an underscore, stating that the property is private and can not be used or accessed directly from outside the class. In the method using internal class is self .__ privateattrs.
 
Private methods of class
__privatemethods (self): two begins with an underscore, the method is declared as private can not be called outside the class. The method call inside the class is self .__ privatemethods ().
 
 
The sample code
class JustCounter:
    __secretCount = 0 # private property, prefixed by two underscores
    publicCount = 0
 
    def count(self):
       self .__ secretCount + = 1 # call the private property within the class
       self.publicCount += 1
       print self.__secretCount  
       
    def __print1 (self): # private methods, two underscores prefix
       print 'This is a private method!'
       
    def print2(self):
       Internal self .__ print1 () # class call a private method
 
counter = JustCounter()
 
print counter.publicCount
#print counter .__ secretCount # error, instances can not access private property
print counter._JustCounter__secretCount # can be accessed via a private property (the name of the object class name __ ._ private property name)
counter.count()
#counter .__ print1 () # error, instances can not access private methods
counter._JustCounter__print1 () # access to the private methods (object name ._ __ private class name method name)
counter.print2()
 
Output:
0
0
1
This is a private method!
This is a private method!
 
 

Guess you like

Origin www.cnblogs.com/myshuzhimei/p/11767352.html