Skip to content

Instantly share code, notes, and snippets.

@brydavis
Last active August 27, 2019 17:53
Show Gist options
  • Save brydavis/6e67ee55da925787de7969c78414ea43 to your computer and use it in GitHub Desktop.
Save brydavis/6e67ee55da925787de7969c78414ea43 to your computer and use it in GitHub Desktop.
Examples creating and using custom context managers in Python
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@brydavis
Copy link
Author

Note that in the __exit__ method using the class way:

def __exit__(self, exit_type, exit_val, exit_tb):
    print("exiting context")

exit_type is the exit type
exit_val is the exit value
exit_tb is the exit traceback

Try printed them out if you want to see what they look like.

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