alternative to with statement?

Craig Yoshioka craigyk at me.com
Tue Feb 28 16:04:59 EST 2012


I see that there was previously a PEP to allow the with statement to skip the enclosing block... this was shot down, and I'm trying to think of the most elegant alternative.
The best I've found is to abuse the for notation:

for _ in cachingcontext(x):
    # create cached resources here
# return cached resources

I would have really liked:

with cachingcontext(x):
    # create cached resources here
# return cached resources

I'd also like to avoid the following because it is unnecessary boilerplate:

with cachingcontext(x) as skip:
    if not skip:
         # create cached resources here
# return cached resources






More information about the Python-list mailing list