Python Lesson 19 (package)

Python Lesson 19 (package) >>> mind FIG >>> in two youth

Package

What is the package?

    The complex is ugly, privacy details hidden to the internal and external provides a simple interface to use

    Internal External hide implementation details, and provide access interface

When should the package?

    When there is some data you do not want the outside world can be directly modified when

    When there are functions that do not want the outside world to use

Why packaging?

    1. In order to ensure the security of critical data

    2. External hide implementation details, the complexity of isolation

grammar

class Person:
    def __init__(self,id_number,name,age)
        self.__id_number = id_number
        self.name = name
        self.age = age
   
p = Person("1111111111111","jack",29)
p.id_number = "222"

The contents of the package features:

    1. The outside world can not directly access

    2. Internal can still use

Competence

Learning package after you can control the property rights

In just two permissions python

    1. disclosure. The default is public

    2. The private, can only be used by the current class of their own

Access to private content in the outside world

    Although the property is enclosed, but still want to use, how to access the outside world

    Modifications and complete access to the private attributes defined by the class method

"" " 
This is a downloader class attributes required to provide such a cache size 
cache memory size can not exceed the limit 
," "," 
class Downloader:
     DEF  the __init__ (Self, filename, URL, buffer_size): 
        self.filename = filename 
        self.url = url 
        Self. __buffer_size = buffer_size
     DEF start_download (Self):
         IF Self. __buffer_size <= 1024 * 1024 :
             Print ( " start downloading .... " )
             Print ( " current buffer size " , Self.__buffer_size )
         the else :
             Print ( " Memory blown up! " )
     DEF set_buffer_size (Self, size):
         # You can add additional logic in the method 
        IF  not of the type (size) == int:
             Print ( " Big Brother buffer must be an integer " )
         the else :
             Print ( " buffer size changed successfully! " ) 
            Self. __buffer_size = size
     DEF get_buffer_size (Self):
         return Self. __buffer_size
D = Downloader ( " gourd " , " http://www.baicu.com " , 1024 * 1024 )
 # modified by a function inside the package takes property 
d.set_buffer_size (1024 * 512 )
 # By accessing property function inside the package 
Print (d.get_buffer_size ()) 
d.start_download ()
View Code

In this way we can modify this key in the external data, do some limitations

Principle achieve package python

That is, when loading the class, the class name _ __ __ replaced with

python is generally not mandatory program must be how how is

External to internal implementation details hidden, and provide access interface

benefit

    1. improve security

    2. Isolation complexity

Syntax : the front for encapsulating property or method name with the double underline

Access the hidden attribute

    To provide a method for accessing and modifying

The method of using a decorator property may be disguised as normal cis properties, properties consistent method call between newspapers

The principle of the package, replacing variable names

property decorator

To modify or access the property by the method itself is no problem, but this is subject to the user's trouble.

Use must know what is common property, which is private property, you need to call them in different ways

property decoration is to make a consistent way to call

There are three related decorator

. 1 .property The apparatus is used in a method of obtaining the property 
 2 . @ Key.setter Used mounted on the method of modifying the properties of 
 . 3 . The @ key.deleter mounted on the deleted property is used in a method of 
note: key property is decorated name of the method is the name of the property's 
interior creates an object variable name is the name of the function   
so when using setter and deleter must ensure that the name of the object using the method of taking calls 
and so is key.setter

Case

class A:
    def __init__(self,name,key):
        self.__name = name
        self.__key = key
    @property
    def key(self):
        return self.__key
    @key.setter
    def key(self,new_key):
        if new_key <= 100:
            self.__key = new_key
        else:
            print("key 必须小于等于100")
    
    @key.deleter
    def key(self):
        Print ( " not allowed to delete the attribute " )
         del Self. __key 
        
A = A ( " Jack " , 123 )
 Print (a.key) 
a.key = 321
 Print (a.key)
View Code

property can be used to calculate properties achieved

    Computing refers to the property: value of the property, not directly available, can be obtained by computing

    For example: square area demand

interface

The interface is a collection of functions, but only the name contained in the interface function does not contain specific implementation code

The interface is essentially a set of protocol standards, follow the standard object can be called

Interface purpose is to improve the scalability:

For example, specify in advance to develop a computer USB interface protocol, as long as you follow the protocol, your equipment can be used by the computer, do not need to care about in the end is a mouse or keyboard

Case

class the USB:
     DEF Open (Self):
         Pass 
    DEF use Close (Self):
         Pass 
    DEF   the Read (Self):
         Pass 
    DEF the Write (Self):
         Pass 
class Mouse (the USB):
     DEF Open (Self):
         Print ( " mouse power .. ... " )
     DEF use Close (Self):
         Print ( " mouse shut down ... " )
     DEF the Read (Self):
         Print ( " get the cursor position .... " )
     DEFWrite (Self):
         Print ( " mouse does not support writing .... " )
 DEF PC (usb_device): 
    usb_device.open () 
    usb_device.read () 
    usb_device.write () 
    usb_device.close () 
m = Mouse ()
 # the computer mouse passed to 
PC (m)
 class keyBoard (the USB):
     DEF Open (Self):
         Print ( " keyboard Power ..... " )
     DEF use Close (Self):
         Print ( " keyboard shut down ... " )
     DEF the Read (Self):
         Print( " Get the key character .... " )
     DEF Write (Self):
         Print ( " can be written to the light color .... " )
 # to the object is a keyboard 
K = KeyBoard () 
PC (K)
View Code

In the above case, PC code Once completed, the late matter what kind of device simply follow the USB interface protocol, can be called computer

The main interface to facilitate the user object is to reduce the user's learning curve, just learning to use a method, it is possible to change the status quo

problem:

If the subclass does not follow your protocol design, there is no way to limit him, will not lead to code running

Abstract class

It refers to an abstract method comprising (a method not function body) class,

Abstract methods defined in subclass must be restricted class: role

Finally: python generally do not restrict how you have to write, as a good programmer, you should consciously abide by the relevant agreements

So with such a type of duck, he said:

If the object looks like a duck, walks like a duck, then he is a duck

You just make sure you write the classes in accordance with the relevant protocol class, you can also achieve the purpose of improving scalability

Case:

class Mouse:
     DEF Open (Self):
         Print ( " mouse power ..... " )
     DEF use Close (Self):
         Print ( " mouse shut down ... " )
     DEF the Read (Self):
         Print ( " get the cursor location .... " )
     DEF the write (Self):
         Print ( " mouse does not support writing .... " )
 DEF PC (usb_device): 
    usb_device.open () 
    usb_device.read () 
    usb_device.write () 
    usb_device. Close () 
m= Mouse ()
 # will be passed to the computer mouse 
PC (m)
 class KeyBoard:
     DEF Open (Self):
         Print ( " Keyboard Power ..... " )
     DEF use Close (Self):
         Print ( " keyboard off the computer ... " ) 
        
    DEF Read (Self):
         Print ( " Get the key character .... " ) 
        
    DEF write (Self):
         Print ( " can be written to the light color .... " ) 
        
# to the object is a keyboard 
K = keyBoard () 
PC (k) 
classUdisk:
     DEF Open (Self):
         Print ( " U disk to start the ... " )
     DEF use Close (Self):
         Print ( " U disk closed ... " )
     DEF the Read (Self):
         Print ( " read data " )
     DEF write (Self):
         Print ( " write data " ) 
U = udisk () 
PC (U)
View Code

Interface is a protocol specification, which features clear who should have subclasses

An abstract class is a subclass mandatory must be realized according to a predetermined protocol

However, python limit your grammar is not respected, we can design the type to duck, both for a number of different classes of objects have the same properties and methods

For users, it may be maintaining the status quo, easy to use various objects

 END

Guess you like

Origin www.cnblogs.com/renvip/p/11257242.html