mirror of
				https://github.com/pocketpy/pocketpy
				synced 2025-10-31 08:50:17 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			38 lines
		
	
	
		
			586 B
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			38 lines
		
	
	
		
			586 B
		
	
	
	
		
			Python
		
	
	
	
	
	
| path = []
 | |
| 
 | |
| class A:
 | |
|     def __init__(self, x):
 | |
|         self.x = x
 | |
|         self.path = []
 | |
| 
 | |
|     def __enter__(self):
 | |
|         path.append('enter')
 | |
|         return self.x
 | |
|     
 | |
|     def __exit__(self, *args):
 | |
|         path.append('exit')
 | |
|     
 | |
| 
 | |
| with A(123):
 | |
|     assert path == ['enter']
 | |
| assert path == ['enter', 'exit']
 | |
| 
 | |
| path.clear()
 | |
| 
 | |
| with A(123) as a:
 | |
|     assert path == ['enter']
 | |
|     assert a == 123
 | |
|     path.append('in')
 | |
| assert path == ['enter', 'in', 'exit']
 | |
| 
 | |
| path.clear()
 | |
| 
 | |
| with A(123) as a:
 | |
|     assert path == ['enter']
 | |
|     -> end
 | |
|     path.append('in')
 | |
| 
 | |
| == end ==
 | |
| assert path == ['enter']
 | |
| 
 |