Python Context Manager

From NovaOrdis Knowledge Base
Revision as of 00:17, 29 August 2022 by Ovidiu (talk | contribs) (→‎Overview)
Jump to navigation Jump to search

External

Internal

Overview

An execution context is useful when the code uses resources that need closing, even if the code follows an unexpected execution path, like when an exception is raised.

An execution context is equivalent with the following construct:

r = Resource()
r.open()
try:
  #
  # use resource, while anywhere in this code block exception might be thrown
  #
finally:
  r.close()