alternative to with statement?

Craig Yoshioka craigyk at me.com
Tue Feb 28 17:22:46 EST 2012


It is a bit non-normal.  but I think this is a good use case as I want to create a very simple-to-use system for non-python experts to safely wrap their CLI programs in a caching architecture... that's why I lament the inability to not use the more streamlined 'with' syntax–  abusing the for loop might just be confusing.  The with statement is also a good fit because the caching strategy does have to atomically acquire, create and release the appropriate locks.  With this statement the cached CLI wrappers can be called from simultaneously from different scripts and still coordinate their activity, by waiting for each other to finish, and reusing the cached results, etc.




 
On Feb 28, 2012, at 1:04 PM, 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.
> 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
> 
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list