[New-bugs-announce] [issue17788] Add a with expression, for use in comprehensions

Eric Wieser report at bugs.python.org
Thu Apr 18 12:02:40 CEST 2013


New submission from Eric Wieser:

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)
    }

----------
messages: 187232
nosy: Eric.Wieser
priority: normal
severity: normal
status: open
title: Add a with expression, for use in comprehensions
type: enhancement

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue17788>
_______________________________________


More information about the New-bugs-announce mailing list