[BangPypers] How to run a block of code just before the interpreter is being exited like END{} in perl ??

Anand Chitipothu anandology at gmail.com
Tue Sep 3 02:57:25 CEST 2013


> This is another version of this by mucking around directly
> with sys.exitfunc using a with context.
>
> from contextlib import contextmanager
>
> @contextmanager
> def end_of_days():
>     def wrapper():
>         print 'Goodbye.'
>     yield wrapper
>
> with end_of_days() as sys.exitfunc:
>     print 3+4
>

How is this different from:

import sys
def goodbye():
    print "Goodbye"

sys.exitfunc = goodbye

I don't think we are really doing anything with the context in your example
except initializing sys.exitfunc.

Anand


More information about the BangPypers mailing list