~/talk

The Enters and Exits of Context Managers

Talkat PyOhio 2021 · on Saturday, 2021-07-31· [python]
slides:none on file

Covers how Python context managers implement resource setup and teardown using __enter__/__exit__ with exact signatures (exit gets exc_type, exc_val, traceback and must return a Boolean). Shows a concrete broken example that replaces sys.stdout to uppercase output, fixes it by saving and restoring the original stream in exit, and demonstrates returning True to suppress exceptions so execution continues. Also shows how to reimplement open with __init__, __enter__ returning the file handle, and __exit__ closing it.

AI-written summary · about

  1. 0:00Start
  2. 0:46Context Managers Use Cases
  3. 1:10Context Managers Class Implementation
  4. 2:20Context Managers __enter - Method Examples
  5. 3:02Context Managers exitmethod
  6. 3:20exit.Method Example
  7. 3:56Context Managers. exit_Exceptions
  8. 4:08Context Managers - exit_. Exceptions
  9. 4:35Context Managers that Take Parameters
~/search
type to search 6,249 conferences, 138,987 speakers and 185,172 talks
~/help
cmdctrl k
search every conference, speaker and series
/
focus the filter search
h
home
c
conferences
s
speakers
e
series
t
light / dark — the status bar button does the same
?
this help
esc
close this, then clear every filter — or just the search, while you are typing in it

Ignored while you are typing. Pages without a filter rail have no search to focus.