Advertisement
Guest User

Untitled

a guest
Oct 26th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. import requests
  2.  
  3. class ControlDel():
  4. def __init__(self):
  5. print("Creating session.")
  6. self.session = requests.Session()
  7. def __del__(self):
  8. print("Closing session.")
  9. self.session.close()
  10. def doX(self):
  11. print("Doing X.")
  12. return True
  13. def doY(self):
  14. print("Doing Y.")
  15.  
  16. class ControlWith():
  17. def __init__(self):
  18. print("Creating session.")
  19. self.session = requests.Session()
  20. def __enter__(self):
  21. return self
  22. def __exit__(self, exc_type, exc_value, traceback):
  23. print("Closing session.")
  24. self.session.close()
  25. def doX(self):
  26. print("Doing X.")
  27. return True
  28. def doY(self):
  29. print("Doing Y.")
  30.  
  31. print("Beginning ControlDel run.")
  32. c1 = ControlDel()
  33. for _ in range(3):
  34. foo = c1.doX()
  35. if foo:
  36. c1.doY()
  37. del c1
  38. print("End of ControlDel run.")
  39.  
  40. print("Beginning ControlWith run.")
  41. with ControlWith() as c2:
  42. for _ in range(3):
  43. foo = c2.doX()
  44. if foo:
  45. c2.doY()
  46. print("End of ControlWith run.")
  47.  
  48.  
  49. #output:
  50. # Beginning ControlDel run.
  51. # Creating session.
  52. # Doing X.
  53. # Doing Y.
  54. # Doing X.
  55. # Doing Y.
  56. # Doing X.
  57. # Doing Y.
  58. # Closing session.
  59. # End of ControlDel run.
  60. # Beginning ControlWith run.
  61. # Creating session.
  62. # Doing X.
  63. # Doing Y.
  64. # Doing X.
  65. # Doing Y.
  66. # Doing X.
  67. # Doing Y.
  68. # Closing session.
  69. # End of ControlWith run.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement