You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We already have raisef and tryf, functional loops, the do[] macro to write imperative code in an expression position... the last missing major piece of the puzzle is to support invoking context managers in an expression position.
So, in a future release, let's add a withf, in the spirit of the already existing tryf.
Single CM or sequence. Pass either a bare context manager or a tuple (cm1, cm2, ...). Multiple CMs are entered left-to-right and exited in reverse, analogously to with cm1, cm2, ...:. Implementation uses contextlib.ExitStack for correct exit ordering and exception propagation.
Body arity auto-detected. If body accepts as many positional arguments as there are CMs, it receives the as-values, in order; if it is a 0-arg thunk, the as-values are discarded (the with lock: ... style). Same convention as tryf's exception handler.
Return value passes through. Whatever body returns is what withf returns. with is treated as an expression here even though Python's statement form is value-less — value of everything, cost of nothing.
The rant in doc/design-notes.md is updated; the lambda-limitations bullet now points at withf instead of "we'll add this someday".
Activity
excutil: add `withf`, `with`-block as a function (#76)
Technologicat commentedon May 5, 2026
Done in
b6423e7(master, will land in 2.2.0).Summary of the design as shipped:
(cm1, cm2, ...). Multiple CMs are entered left-to-right and exited in reverse, analogously towith cm1, cm2, ...:. Implementation usescontextlib.ExitStackfor correct exit ordering and exception propagation.bodyaccepts as many positional arguments as there are CMs, it receives the as-values, in order; if it is a 0-arg thunk, the as-values are discarded (thewith lock: ...style). Same convention astryf's exception handler.bodyreturns is whatwithfreturns.withis treated as an expression here even though Python's statement form is value-less — value of everything, cost of nothing.The rant in
doc/design-notes.mdis updated; the lambda-limitations bullet now points atwithfinstead of "we'll add this someday".— Claude (Opus 4.7)
briefs: update 2.2.0-remaining-issues for next-session handoff
Release 2.2.0 "Hail Eris"