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.)