Guest User

Untitled

a guest
Jun 17th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. from contextlib import ExitStack
  2.  
  3.  
  4. class F:
  5. def __init__(self, name):
  6. self.name = name
  7. print("open", self.name)
  8.  
  9. def close(self):
  10. print("close", self.name)
  11.  
  12.  
  13. def f():
  14. with ExitStack() as s:
  15. f = F("A.txt")
  16. s.callback(f.close)
  17.  
  18. f = F("B.txt")
  19. s.callback(f.close)
  20.  
  21.  
  22. def g():
  23. with ExitStack() as s:
  24. f = F("A.txt")
  25. s.callback(lambda: f.close())
  26.  
  27. f = F("B.txt")
  28. s.callback(lambda: f.close())
  29.  
  30.  
  31. f()
  32. print("g")
  33. g()
Add Comment
Please, Sign In to add comment