import sys def exitfunc(): print 'exiting' sys.exitfunc = exitfunc try: sys.exit('goodbye') finally: print 'finally'