Skip to content

Instantly share code, notes, and snippets.

@evansde77
Created August 18, 2019 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evansde77/a139aaf2ce0b173fe02b9ec3a0ca64b2 to your computer and use it in GitHub Desktop.
Save evansde77/a139aaf2ce0b173fe02b9ec3a0ca64b2 to your computer and use it in GitHub Desktop.
from contextlib import contextmanager
@contextmanager
def context1(arg):
print("Bronte")
yield "Tolkien"
print(arg)
class Context2:
def __init__(self, arg):
self.arg = arg
print("Austen")
def print_arg(self):
print(self.arg)
def __enter__(self):
print("Orwell")
return self
def __exit__(self, *args):
if args:
print("Wordsworth")
else:
print("Conan Doyle")
return True
with Context2("Dickens") as ctx2:
with context1("Kipling") as a:
print("Wells")
print(a)
ctx2.print_arg()
raise RuntimeError("Shelley")
# What authors get printed in what order?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment