Dynamic Wind
def wind
- wind(
- before: () → AbstractContextManager[T, B],
- after: (...) → Any | None = None,
- *,
- auto_exit: Literal[True] = True,
- wind(
- before: () → AbstractContextManager[T, B],
- after: (...) → Any | None = None,
- *,
- auto_exit: Literal[False],
- wind(
- before: () → T,
- after: (...) → Any | None = None,
- wind(
- before: None = None,
- after: (...) → Any | None = None,
class wind_range
- class wind_range(stop: int, /)
- class wind_range(start: int, stop: int, step: int = 1, /)
Bases:
WindBase[wind_range,int]Context manager providing multi-shot-safe range iteration.
Usage:
with wind_range(10) as r: for i in r: v = choose() # multi-shot safe
On multi-shot re-entry, the iterator position is restored to the value it had when the continuation was captured, so the
forloop resumes from the correct position.- __iter__() wind_range
class WindBase
- class WindBase[T, S]
-
Base class for dynamic-wind context managers.
Subclasses implement the
_wind_*protocol methods. The base class manages the wind stack and drives the snapshot/restore lifecycle for multi-shot continuations.T: type of the value yielded by
__enter__(theastarget) S: type of the snapshot state for multi-shot re-entryProtocol methods:
_wind_enter()Called on initial entry and on multi-shot re-entry. Returns the value that
__enter__yields (theastarget)._wind_exit(exc_type, exc_val, exc_tb)Called on exit. Returns
Trueto suppress the exception._wind_snapshot()Called at continuation capture time. Returns an opaque value that will be passed to
_wind_restoreon re-entry. Default: returnsNone._wind_restore(state)Called before
_wind_enteron multi-shot re-entry. Receives the value returned by_wind_snapshotat capture time. Default: no-op.
class Ref
- class Ref[T]
-
Indirect reference returned by
windcontext manager.windwraps the return value ofbefore()in aRefso that multi-shot continuation resumes can update the underlying value. Useunwrap()to retrieve the current value:with wind(lambda: open("path.txt")) as ref: ref.unwrap().read()
On multi-shot re-entry,
before()is called again and the sameRefobject is updated with the new return value. Because theRefis a heap object shared across shots,unwrap()always returns the latest value even after frame restoration.- unwrap() T