context manager

An object that sets something up and later tears it down, designed to be used with a with statement.

In more detail

Files, locks and database connections are common context managers. The clean-up runs however the block is left, normally or through an exception.

Examples

open() returns a file object that is also a context manager.
with pytest.raises(ValueError):
    discounted(1000, 120)

Related terms

Taught in

References