Skip to content
masterclass Buddy

Python · Concept

with

The with statement runs a block with a context manager. It calls __enter__() before the block and __exit__() afterward, so setup and cleanup stay together.

Last updated

Syntax

with expression [as target]: suite

In-depth API docs are only for subscribers: parameters, examples, common mistakes and interview practice.

Subscribe
  • __enter__()
  • __exit__()
  • context manager
  • try statement
  • contextlib

Source: 8.5. The with statement — Python 3.15.2 documentation. Checked 2026-11-03.