alternative to with statement?

Prasad, Ramit ramit.prasad at
Tue Feb 28 23:12:45 CET 2012

Craig Yoshioka wrote:
>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. [..]

>I would have really liked:
>with cachingcontext(x):
>    # create cached resources here
># return cached resources

Is this a common pattern? I thought the point of the context manager 
was to remove create and close the resources (like with file opening).
Seems slightly odd to use just for creation...but maybe only because
I have never used it like that.


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at  

More information about the Python-list mailing list