register cleanup handler

Neal Becker ndbecker2 at
Fri Jul 24 16:57:30 CEST 2015

I have code like:

if (condition):


<cleanup has to happen here if required, even if above did return, continue 
or exception>

Now, how can I make sure cleanup happens?  Actually, what I really would 
like, is:

if (condition):
  register_scoped_cleanup (cleanup_fnc)


So, any thoughts/hopes of python being able to do something like this?

I know we have try/finally, but I don't think that helps here, because
code_executed_unconditionally couldn't be inside the try.  Or am I missing 
something obvious?

More information about the Python-list mailing list