Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #21013

Re: alternative to with statement?

Subject Re: alternative to with statement?
From Craig Yoshioka <craigyk@me.com>
Date 2012-02-28 14:22 -0800
References <DF227815-9BD4-403D-A7DF-1DB8717026C7@me.com>
Newsgroups comp.lang.python
Message-ID <mailman.268.1330471410.3037.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: alternative to with statement? Craig Yoshioka <craigyk@me.com> - 2012-02-28 14:22 -0800

csiph-web