From Martin Heinz


Some Cools Things contextlib Can Do

  1. Trigger an object to close with contextlib.closing
  2. Suppress warnings with contextlib.suppress. Equivalent to:
try:
	  do_something()
except RuntimeError:
    pass
  1. Add __enter__ and __exit__ functionality to functions via the contextlib.ContextDecorator mixin / decorator

There Are Three Types of Context Managers

  1. Single-use
  2. Reusable
  3. Reentrant—Reusable, and can be nested 🤯