Context Manager __enter__ and __exit__

Directly on the code:

. 1  Import OS
 2  
. 3  class the Open (Object):
 . 4      DEF  the __init__ (Self, File):
 . 5          self.file = File
 . 6  
. 7      DEF  the __enter__ (Self):
 . 8          Print ( ' instantiated call this code block, if used as examples of the object to the object is returned as a ' )
 . 9          self.f = Open (self.file)
 10          return Self   # returns an instance method is to call the class 
. 11  
12 is      DEF  the __exit__ (Self, exc_type, exc_val, exc_tb):   # three parameters representing the type of error, error value, error tracking the traceback calls out 
13         Print ( ' the Exit ... ' )
 14          Print (exc_tb)
 15          Print (exc_type)
 16          Print (exc_val)
 . 17          self.f.close ()
 18 is  
. 19      @Property    # decorator to invoke property methods of embodiment 
20 is      DEF say_name ( Self):
 21 is          return os.path.dirname (os.path.abspath with ( __FILE__ ))
 22 is  
23 is  
24  IF  the __name__ == ' __main__ ' :
 25      with the Open (R & lt ' bin / aa.py ') As F:   # by way of example with class, method triggers __enter__ class, if there __enter__ return value, then as the return value assigned to an object 
26 is          Print ( ' ====== ' )
 27          Print (f.say_name)
 28          Print ( ' ------ ' )   # above finished block method __exit__ trigger 
29          Print (ABCD)   # where the variable is not present abcd, the program throws an exception and termination, but because there are __exit__ method, an error message will be swallowed, and direct execution method __exit__ 
30          Print ( ' ..... ' )   # on a line throwing exceptions, will not be executed here 
31      Print ( ' lalala ' )   # Throws on the line, here will not be executed

operation result:

C: \ the Users \ lwj, \ AppData \ the Local \ Programs \ the Python \ Python37 \ python.exe D: / 02Project / Pacho / 12day / ST_ Context Management .py 
instantiated call this code block, if used as return Example object to the object as
 ====== 
D: \ 02Project \ Pacho \ 12day
 ------ 
Exit ...
 <the traceback calls out AT 0x0000000001FFDB48 Object> 
< class  ' NameError ' > 
name ' ABCD '  iS  not defined 
Traceback ( Recent Last Call MOST): 
  File " D: / 02Project / Pacho / 12day / ST_ context management .py " , Line 29, in <Module1>
     Print (ABCD)   #Here there is no variable abcd, the program will throw an exception and termination, but because there are __exit__ method, an error message will be swallowed, and direct execution method __exit__ 
NameError: name ' ABCD '  IS  not defined 

Process Finished with Exit code 1

 

Below that is the return value __exit__, for example:

. 1  Import OS
 2  
. 3  class the Open (Object):
 . 4      DEF  the __init__ (Self, File):
 . 5          self.file = File
 . 6  
. 7      DEF  the __enter__ (Self):
 . 8          Print ( ' instantiated call this code block, if used as examples of the object to the object is returned as a ' )
 . 9          self.f = Open (self.file)   # here is to be able to call close () method 
10          return Self   # returns an instance method is to call the class 
. 11  
12 is      DEF  the __exit__ (Self, exc_type, exc_val, exc_tb):   #Three parameters representing the type of error, error value, error tracking the traceback 
13 is          Print ( ' the Exit ... ' )
 14          Print (exc_tb)
 15          Print (exc_type)
 16          Print (exc_val)
 . 17          self.f.close ()
 18 is          return True   # If the exit returns True if swallowed all ends abnormally and ends with the statement; if the exit returns False, then spit out the anomaly 
19  
20      the @Property    # decorator, as properties of the method call 
21      DEF say_name (Self):
 22          return os. path.dirname (os.path.abspath with ( __FILE__ ))
 23 is  
24  
25  IF the __name__ == ' __main__ ' :
 26 is      with the Open (R & lt ' bin / aa.py ' ) AS F:   # instance with by way of classes, the class method __enter__ triggered, if there __enter__ return value, as the return value is assigned to an object, i.e., f = Enter __ example .__ () 
27          Print ( ' ====== ' )
 28          Print (f.say_name)
 29          Print ( ' ------ ' )   # above code block is finished trigger __exit__ method 
30          Print (ABCD)   # here does not exist variable abcd, the program will throw an exception and termination, but because there are __exit__ method, an error message will be swallowed, and direct execution __exit_ _ The method 
31 is          Print ( '..... ' )   # line throw abnormality, since return exit is True, this line is executed 
32      Print ( ' lalala ' )   # on line throw an exception because return exit is True, this line is executed

Execution of results:

. 1 C: \ the Users \ lwj, \ AppData \ the Local \ Programs \ the Python \ Python37 \ python.exe D: / 02Project / Pacho / 12day / ST_ Context Management .py
 2  instantiated call this code block, if used as the returns to the object as an object instance
 . 3 ======
 . 4  D: \ 02Project \ Pacho \ 12day
 . 5 ------
 . 6  the Exit ...
 . 7 <AT 0x000000000200DB08 the traceback Object>
 . 8 < class  ' NameError ' >
 . 9 name ' ABCD '  IS  Not defined
 10  lalala
 . 11  
12 is Process Finished Exit with code 0

 

Guess you like

Origin www.cnblogs.com/wjlv/p/11594092.html