I'm switching from Ruby to Python for a project. I appreciate the fact that Python has first-class functions and closures, so this question ought to be easy. I just haven't figured out what is idiomatically correct for Python:
In Ruby, I could write:
def with_quietude(level, &block)
begin
saved_gval = gval
gval = level
yield
ensure
gval = saved_gval
end
end
and call it like this:
with_quietude(3) {
razz_the_jazz
begin_the_beguine
}
(Note: I'm not asking about Python try/finally
handling nor about saving and restoring variables -- I just wanted a non-trivial example of wrapping a block inside some other code.)
update
Or, since some of the answers are getting hung up on the global assignments in the previous example when I'm really asking about closures, what if the call was as follows? (Note that this doesn't change the definition of with_quietude):
def frumble(x)
with_quietude {
razz_the_jazz(x)
begin_the_beguine(2 * x)
}
end
How would you implement something similar in Python (and not get laughed at by the Python experts)?