Packaging
1. Why encapsulation?
Encapsulation is to hide the specifics of the details of the data properties and methods, providing only one interface. Encapsulation can be used without caring about how the object is built
2. Encapsulation includes the encapsulation of the data and the encapsulation of the function, the encapsulation of the data is to protect the privacy, the encapsulation of the function is to isolate the complexity
3. The encapsulation of the data is to precede the attribute with a __
Class People:def __init__ (self,name,age,salary): self.name=name self.age=age self.__salary=salaryp= People (' Zhang ', 19,100000) print (p.name) #zhangprint (p.age) #19print (p.__salary) #AttributeError: ' People ' object has No attribute ' __salary '
Hey, error, let's open the object's namespace and see what happened.
Print (p.__dict__) #{' name ': ' Zhang ', ' age ': +, ' _people__salary ': 100000}
Oh, it turns out that Python changed __salary into _people__salary, and then again.
Print (p._people__salary) #100000
So, Python is not absolutely hidden, as long as you know the above, it does not matter hidden
These deform operations occur only during the definition phase of the class or the object definition (instantiation phase) stage
Although there is no direct access to the properties of the __, but within the class can be accessed, you can understand that, in the definition phase, as long as the beginning of the Python interpreter is recognized as the _ Class name __ property, so within the class is accessible, so we can do a little bit of things
Look at this first.
Class A:def foo (self):p rint ("from a foo") Self.bar () def bar (self):p rint ("from a Bar") class B (a):d EF bar (self):p rint (' from B Bar ') b=b () B.foo () #from A foo
#from B Bar do not think much of the call function when the definition of position, to see the call location
What if you want to call the bar () function of the parent class? How to do it
Class A:def foo (self):p rint ("from a foo") Self.__bar () def __bar (self):p rint ("from a Bar") class B (a):d ef __bar (self) :p rint (' from B Bar ') b=b () B.foo () #from a foo#from a bar have no experience programming enjoyment
4. Application of the package
1) do not let the outside world see how our data attributes are defined, only through the interfaces we provide, to see what we allow the outside world to see
Class People:def __init__ (self,name,age,height,weight,hobby): self.__name=name self.__age=age self._ _height=height self.__weight=weight self._hobby=hobbydef tell_info (self):p rint ("' Name:%s age:%s height:%s weeight:%s '% (self.__name,self.__age, self.__height,self.__weight)) p=people (' Zhang ', 18,1.90,75, ' read ') P.tell_info ()
2) The more common scenario is that we can limit the type of data, add our own logic and then encapsulate it to the user
def tell_name (self):p rint (self.__name) #修改名字def set_name (self,new): If not isinstance (NEW,STR): Raise TypeError (' The name must be a String type ') self.__name=new
5. Look at the above operation, the user to view the name of the time also have to P.tell_name (), is a data property, but we made it into a function, how to disguise it, you can use the property of the built-in function
Class People:def __init__ (self,name,age,height,weight,hobby): self.__name=name self.__age=age self._ _height=height self.__weight=weight self._hobby=hobby @propertydef name (self): return self.__namep= People (' Zhang ', 18,1.90,75, ' read ') print (p.name) #zhang
Data properties should also have modifications, delete operations
@propertydef Name: Return Self.__name#name has been modified by the property, there are setters and Deleter @name. Setterdef name (self,new) : If not isinstance (NEW,STR): Raise TypeError (' name must be String type ') self.__name=new @name. Deleterdef name (self):d el SELF.__NAMEP = People (' Zhang ', ' 1.90 ', ' read ') print (p.name) #zhangp. Name= ' Can ' #修改print (p.name) #candel P.name #删除print (p.name) #AttributeError: ' People ' object has no attribute ' _people__name '