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


Groups > comp.lang.python > #21013 > unrolled thread

Re: alternative to with statement?

Started byCraig Yoshioka <craigyk@me.com>
First post2012-02-28 14:22 -0800
Last post2012-02-28 14:22 -0800
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#21013 — Re: alternative to with statement?

FromCraig Yoshioka <craigyk@me.com>
Date2012-02-28 14:22 -0800
SubjectRe: alternative to with statement?
Message-ID<mailman.268.1330471410.3037.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web