Good python equivalent to C goto

Carl Banks pavlovevidence at gmail.com
Sun Aug 17 01:59:53 EDT 2008


On Aug 17, 12:35 am, Michael Torrie <torr... at gmail.com> wrote:
> However it's not necessary in python to do any of this, since you can
> define nested functions that have access to the parent scope.  Anytime
> you need to clean up, just call the nested cleanup function and then return.

That is unnecessary and dangerous in Python.

98% of the time the Python interpreter cleans up what you would have
had to clean up by hand in C.

The rest of the time you should be using a try...finally block to
guarantee cleanup, or a with block to get the interpreter do it for
you.

Calling a nested function to perform cleanup is prone to omission
errors, and it doesn't guarantee that cleanup will happen if an
exception is raised.


Carl Banks



More information about the Python-list mailing list