Skip to content

Instantly share code, notes, and snippets.

@bgilbert
bgilbert / contextmanager.md
Last active June 6, 2024 08:58
Python context managers

Context managers

In Python, a context manager is an object that can be used in a with statement. Here's a context manager that reports the total wall-clock time spent inside a with block:

import time

class Timer(object):
    def __init__(self, msg):
        self._msg = msg