2021-01-10 17:39:05 +03:00
|
|
|
from fileio import open
|
|
|
|
|
|
|
|
class ContextManager:
|
|
|
|
def __init__(title):
|
|
|
|
self.title = title
|
|
|
|
def __enter__():
|
|
|
|
print("Enter context manager", self.title)
|
2021-03-18 14:41:11 +03:00
|
|
|
def __exit__(type,value,traceback):
|
2021-01-10 17:39:05 +03:00
|
|
|
print("Exit context manager", self.title)
|
|
|
|
|
|
|
|
|
|
|
|
def doesThing():
|
|
|
|
print("Creating CM")
|
|
|
|
let cm = ContextManager('simple')
|
|
|
|
|
|
|
|
with cm:
|
|
|
|
print("In context manager")
|
|
|
|
return 42
|
|
|
|
|
|
|
|
print("Not exiting correctly?")
|
|
|
|
return "oh no"
|
|
|
|
|
|
|
|
print(doesThing())
|
|
|
|
|
|
|
|
|
|
|
|
def doesANestedThing():
|
|
|
|
with ContextManager('outer'):
|
|
|
|
with ContextManager('inner'):
|
|
|
|
with ContextManager('triple'):
|
|
|
|
return 42
|
|
|
|
print('Should not print')
|
|
|
|
print('Should not print')
|
|
|
|
print('Should not print')
|
|
|
|
|
|
|
|
print(doesANestedThing())
|