alternative to with statement?

Terry Reedy tjreedy at udel.edu
Tue Feb 28 17:56:45 EST 2012


On 2/28/2012 5:12 PM, Prasad, Ramit wrote:
> 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).

Exactly. It is Python's version of RAII
https://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization

> Seems slightly odd to use just for creation...
I do not see the point either.

-- 
Terry Jan Reedy




More information about the Python-list mailing list