This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add a with expression, for use in comprehensions
Type: enhancement Stage: resolved
Components: Interpreter Core Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Eric.Wieser, barry, berker.peksag, eric.smith
Priority: normal Keywords:

Created on 2013-04-18 10:02 by Eric.Wieser, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg187232 - (view) Author: Eric Wieser (Eric.Wieser) * Date: 2013-04-18 10:02
It would be nice if there was a `with` "expression". Such that instead of:

    with open(...) as f:
        result = foo(f)

One could write:

    result = foo(f) with open(...) as f

This would be particularly useful in comprehensions. Instead of:

    files = {}
    for fname in os.listdir('.'):
        if predicate(fname):
            with open(fname) as f:
                files[fname] = foo(f)

    files = {
        fname: foo(f) with open(fname) as f
        for fname in os.listdir('.') if predicate(fname)
    }
msg187236 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2013-04-18 10:29
I think this needs to be discussed on python-ideas first.
msg187239 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2013-04-18 11:50
Indeed, this should be on python-ideas. Eric: can you start a thread over there? Thanks.
History
Date User Action Args
2022-04-11 14:57:44adminsetgithub: 61988
2013-04-18 13:18:21barrysetnosy: + barry
2013-04-18 11:50:34eric.smithsetstatus: open -> closed

nosy: + eric.smith
messages: + msg187239

resolution: not a bug
stage: resolved
2013-04-18 10:29:49berker.peksagsetnosy: + berker.peksag
messages: + msg187236
2013-04-18 10:03:51Eric.Wiesersetcomponents: + Interpreter Core
2013-04-18 10:02:40Eric.Wiesercreate