Skip to content

Instantly share code, notes, and snippets.

@jacekbj
Created May 9, 2020 17:50
Show Gist options
  • Save jacekbj/8dffc606d8ccd7cfbfdfff38b5bc0900 to your computer and use it in GitHub Desktop.
Save jacekbj/8dffc606d8ccd7cfbfdfff38b5bc0900 to your computer and use it in GitHub Desktop.
Python

From https://jeffknupp.com/blog/2016/03/07/python-with-context-managers/. Can be used both as context manager and as function decorator.

from contextlib import ContextDecorator

class makeparagraph(ContextDecorator):
    def __enter__(self):
        print('<p>')
        return self

    def __exit__(self, *exc):
        print('</p>')
        return False

@makeparagraph()
def emit_html():
    print('Here is some non-HTML')

emit_html()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment