with- context manager

Never mind not close the file.
File is too large, such as FAT323 format, maximum 4G, if exceeded, will not write in, this will close () will throw an exception in front, so no file has been closed, it will cause the file resources will always be occupied, resulting in resource leaks, so there will have to open the file of the phenomenon, leading to resource leaks.

solution:

the try :
     Pass 
the except : 
    Error 
the else : 
    correct 
a finally : 
    use Close ()

File Open / close of the file during the intermediate storage resources / marking (that is, make a mark to indicate that the resource file is used, by whom, where the use of a step, etc.) up, such a situation is called a context.

Format:
  Open
  Resource Save / tag
  close

"" " 
Write a class, complete with operating 

as long as a class that implements the context manager, with to support 

." "" 


Class MyFile:
     "" " file type, replace the open class and open class like " "" 
    DEF  __init__ (Self , name, MODE): 
        the self.name = name 
        self.mode = MODE 

    DEF  the __enter__ (Self):
         "" " it is above environment resources, the resources to the back as the variable " "" 
        Print ( ' moving into the Get text resource ' ) 
        self.file = Open (the self.name, self.mode)
         return Self.net 

    def  __exit__(Self, exc_type, exc_val, exc_tb):
         "" " provided below, close the resource " "" 
        Print ( ' is entering Hereinafter, close the resource ' ) 
        self.file.close () 


IF  the __name__ == ' __main__ ' :
     "" " 
    1.> obj = MyFile ( 'app.txt ', 'rb') performed, initialization is complete a call to the class object obj 
    2.> the method by calling the object obj acquired __enter__ resources <hereinabove> ---> to as behind File 
    3.> with normal code can be used normally acquired resource 
    4> on exit with a block of statements, called when the resource off obj __exit__ method, the process proceeds below, the release of resources 

    "" " 

    with MyFile ( ' app.txt ' , 'rb') as file:
        print(file.read(8)) 

"" " 
Summary: The 
implementation supports operation with class ---> context manager called empathy __iter __ / __ next__ called iterator class 
implement __enter__ method above: to provide resources 
to achieve __exit__ methods below: Close the resource 

__enter__: meaning into the resources 
__exit__: meaning exit resources 
"" "
"" " 
Provides resources to create / destroy the resource context manager is 

" "" 

class myFile:
     "" " file type, replace the open class, open class and similar " "" 

    DEF  __init__ (Self, name, the MODE): 
        self.name = name 
        self.mode = the MODE 

    DEF  __enter__ (Self):   # access to resources 
        . "" " this is to provide resources above environment, providing resources to back as variable " "" 
        Print ( ' are entering the above access to resources ' ) 
        Self .file = Open (the self.name, self.mode)
         return Self.file

    def __exit__(self, exc_type, exc_val, exc_tb):  # Close resource 
        "" " provided below, close the resource 
        exc_type: Type 
        exc_val: Type Value 
        exc_tb: Return Value Tracking 
         " "" 
        Print ( ' is entering Hereinafter, close the resource ' )
         # Print (exc_type) # <class 'io.UnsupportedOperation' > 
        # Print (exc_val) Read # 
        # Print (exc_tb) # <Object AT 0x0000024BC9F1F748 the traceback> 
        self.file.close () 


IF  the __name__ == ' __main__ ' : 
    with myFile ( ' app.txt ' , ' WB ' ) AS File :  #Context is to manage resources 
        Print (File.read (8))

@contextmanager:

from contextlib Import ContextManager   # introduced into Context Manager 


@contextmanager 
DEF myopen (name, MODE):
     "" " generator function decorator + = Context Manager: entering above, access to resources " "" 
    Print ( ' is acquired into the above resources ' ) 
    File = Open (name, the MODE)
     yield File   # pause here, you can not use return, it will direct the resources of all closed 

    # to enter the closed following resources 
    Print ( ' is entering closed following resources ' ) 
    File.close () 


IF  the __name__ == ' __main__ ' :
    with myopen('app.txt', 'rb') as file:
        print(file.read(8))

Guess you like

Origin www.cnblogs.com/huaibin/p/12100363.html