Skip to content

Instantly share code, notes, and snippets.

@wood-push-melon
Created December 11, 2019 15:07
Show Gist options
  • Save wood-push-melon/4379cb9b4939d34c82df0543964e1692 to your computer and use it in GitHub Desktop.
Save wood-push-melon/4379cb9b4939d34c82df0543964e1692 to your computer and use it in GitHub Desktop.
Object that Supports with Statement
class Connection:
def __init__(self):
...
def __enter__(self):
...
def __exit__(self, type, value, traceback):
...
with Connnection as conn:
...
from contextlib import contextmanager
@contextmanager
def tag(name):
print(f'<{name}>')
yield
print(f'')
with tag('h1'):
print('This is the title.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment