Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- class ControlDel():
- def __init__(self):
- print("Creating session.")
- self.session = requests.Session()
- def __del__(self):
- print("Closing session.")
- self.session.close()
- def doX(self):
- print("Doing X.")
- return True
- def doY(self):
- print("Doing Y.")
- class ControlWith():
- def __init__(self):
- print("Creating session.")
- self.session = requests.Session()
- def __enter__(self):
- return self
- def __exit__(self, exc_type, exc_value, traceback):
- print("Closing session.")
- self.session.close()
- def doX(self):
- print("Doing X.")
- return True
- def doY(self):
- print("Doing Y.")
- print("Beginning ControlDel run.")
- c1 = ControlDel()
- for _ in range(3):
- foo = c1.doX()
- if foo:
- c1.doY()
- del c1
- print("End of ControlDel run.")
- print("Beginning ControlWith run.")
- with ControlWith() as c2:
- for _ in range(3):
- foo = c2.doX()
- if foo:
- c2.doY()
- print("End of ControlWith run.")
- #output:
- # Beginning ControlDel run.
- # Creating session.
- # Doing X.
- # Doing Y.
- # Doing X.
- # Doing Y.
- # Doing X.
- # Doing Y.
- # Closing session.
- # End of ControlDel run.
- # Beginning ControlWith run.
- # Creating session.
- # Doing X.
- # Doing Y.
- # Doing X.
- # Doing Y.
- # Doing X.
- # Doing Y.
- # Closing session.
- # End of ControlWith run.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement