Vadim Nasardinov wrote:

> On Monday 05 April 2004 13:59, Charles Comstock wrote:
> 
>>def setup_and_teardown
>>	#code which must execute before code block a and b
>>	yield
>>	#code which must execute after code block a and b
>>end
>>
>>setup_and_teardown() do
>>	# operation a) which may fail but we want setup and cleanup code
>>	# guarenteed
>>end
>>
>>setup_and_teardown() do
>>	# operation b) which may fail but we want setup and cleanup code
>>	# guarenteed
>>end
>>
>>Is there any sort of similar guarded block equivalent in python
>>while still allowing multiple blocks to be called and not requiring
>>a complex dispatcher routine?
> 
> 
> The English language does just fine without explicit case endings.
> The only time you would miss them is when you think in German when
> speaking English.  That's a short summary of this longer post:
> http://groups.google.com/groups?q=Python+try+finally+critical+section+group%3Acomp.lang.lisp
> 
> To answer your specific question, here's one way of doing this. (My
> Python is as bad as my Ruby, so this may not be the best translation
> from Ruby into Python.):
> 
>     def with_critical_resource(function):
>         try:
>             # set up critical resource
>             print "resource opened,", 
>             function()
>         finally:
>             # release critical resource
>             print ",resource closed"
> 
> 
>     def do_stuff():
>         print "code executed",
> 
>     def blow_up():
>         raise "let someone else mop this up"
> 
>     with_critical_resource(do_stuff)
>     with_critical_resource(blow_up)
> 
> (Personally, I find the Ruby block syntax more general in this
> respect, but I would hesitate to draw any far-reaching conclusions
> from this fact alone.  I equally like both languages.)
> 
> 
> 

I like the logic being inline like it is in the ruby example.  I find it 
annoying to jump somewhere else to see what something like this does. 
If the logic stays inline I use it alot more, and so it feels better.

I guess it's just personal preference, but it just seems more readable 
with it inline.

I am impressed by the java solution, I hadn't thought of that, it's 
pretty slick, except for the extra layer of run that java mandates.  Oh 
well at least it's possible, even if it requires that extra couple of 
lines that java always needs.

Charles Comstock