Python 3 Package
From the meaning of the package itself to understand, packaging is like a sack, small fish, shrimp, Xiao Wang Eight, together into a sack, and then seal the sack. According to this logic, encapsulation = ' hiding ', this understanding is quite one-sided.
First see how to hide
Hide a property (set to private) in Python, starting with a double underscore
Actually this is just a kind of morphing operation
Names that begin with all double underscores in a class, such as __x, are automatically formed: _ Class name __x form:
classA:__n=0#The data properties of the class should be shared, but syntactically it is possible to set the data property of the class to private, such as __n, to be transformed into _a__n def __init__(self): self.__x=10#deformation to self._a__x def __foo(self):#deformation to _a__foo Print('From A') defBar (self): self.__foo()#only within the class can you access it in the form of __foo.A._a__n is accessible, that is, this operation is not strictly restricting external access, just a syntactic distortion.
View Code
Features of this automatic deformation:
- The __x of a class definition can only be used internally, such as self.__x, and the result of the transformation is referred to.
- This deformation is in fact the external deformation, outside is not accessible through the name __x.
- The __x defined in the subclass does not overwrite the __x defined by the parent class, because the subclass is formed with the following: _ Subclass name __x, and the parent class is formed: _ Parent class name __x, that is, a property that begins with a double underscore is not overwritten by the child class when it inherits to the subclass.
The problems to be aware of in this deformation are:
1. This mechanism also does not really restrict our direct access to properties from the outside, knowing that thunder and attribute names can be spelled out by name: _ Class Name __ property, which can then be accessed, such as A._a__n
2. The process of deformation is only released once when the class is defined, and the assignment operation after the definition does not deform
3. In inheritance, the parent class can define a method as private if it does not want the subclass to change its own method.
Python does not really prevent you from accessing private properties, the module also follows this convention, if the module name starts with a single underscore, then the From module import * cannot be imported, but you from module import _private_ Module still can be imported in fact, many times you go to invoke the function of a module will encounter a single underscore (Socket._socket,sys._home,sys._clear_type_cache), these are private, in principle, for internal calls. As external can also be used, only to write code more silly, python to be like other programming languages, strictly control the access rights of properties, only with the help of built-in methods such as __getattr__
#first See how to hideclassFoo:__n=111111#_foo__n def __init__(self,name): Self.__name=name#Self._foo__name=name def __f1(self):#_foo__f1 Print('F1') defF2 (self): self.__f1()#self._foo__f1 ()F=foo ('Egon')#print (f.__n)#f.__f1 ()#F.__namef.f2 ()#Tong F #this kind of hiding needs to be noted:#1: This kind of hiding is just a syntactic warp operation, and does not really hide the attributes.Print(Foo.)__dict__)Print(F.__dict__)Print(F._foo__name)Print(f._foo__n)#2: This syntax-level transformation occurs during the class definition phase and occurs only during the class definition phaseFoo.__x=123123123123123123123123123123123123123123Print(Foo.)__dict__)Print(Foo.)__x) F.__x=123123123Print(F.__dict__)Print(F.__x)#3: The __x defined in the subclass does not overwrite the __x defined by the parent class, because the subclass is formed: The subclass name __x, and the parent class is formed: _ The Parent class name __x, that is, when the double-glide line begins with a property that inherits to the child class, the subclass cannot be overwritten. classFoo:def __f1(self):#_foo__f1 Print('foo.f1') defF2 (self): self.__f1()#self._foo_f1classBar (Foo):def __f1(self):#_bar__f1 Print('bar.f1') b=Bar () b.f2 ()#encapsulation is not a pure sense of concealment#1: Encapsulate Data Properties: Hide properties, and then provide access to the interface of properties, the key is that we customize some control logic within the interface to strictly control the use of data propertiesclasspeople:def __init__(self,name,age):if notisinstance (NAME,STR):RaiseTypeError ('%s must be str'%name)if notisinstance (age,int):RaiseTypeError ('%s must be int'%Age ) self.__name=name self.__age= AgedefTell_info (self):Print('< name:%s Age:%s>'% (self.__name, self.__age)) defSet_info (self,x,y):if notisinstance (X,STR):RaiseTypeError ('%s must be str'%x)if notisinstance (y,int):RaiseTypeError ('%s must be int'%y) self.__name=x self.__age=YP=people ('Egon', 18) P.tell_info ()#p.set_info (' Egon ', ' + ')P.set_info ('Egon', 19) P.tell_info ()#2: Encapsulation function Properties: For isolation of complexity#withdrawal is a function, and this function has many functions: card, password authentication, input amount, print bill, withdraw money#for the user, only need to know the withdrawal function, the rest of the functions we can hide, it is obvious that#isolation of complexity and increased securityclassATM:def __card(self):Print('Insert Card') def __auth(self):Print('user authentication') def __input(self):Print('Enter withdrawal amount') def __print_bill(self):Print('Print your Bill') def __take_money(self):Print('Withdrawals') defWithdraw (self): self.__card() self.__auth() self.__input() self.__print_bill() self.__take_money() a=ATM () A.withdraw ()#_x=123
Sample Code
Python 3 Package