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

Anand B Pillai anandpillai at letterboxes.org
Mon Sep 2 21:57:49 CEST 2013


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Monday 02 September 2013 08:09 PM, venkatakrishnan g wrote:
> import atexit
> 
> def callback(): print "about to exit!"
> 
> atexit.register( callback )

Better use it as decorator.

import atexit

@atexit.register
def last_man_standing():
    print "Hasta la vista, Baby."p


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


> 
> 
> On 2 September 2013 20:00, babmis <babmis at outlook.com> wrote:
> 
>> 
>> ______________________________**_________________ BangPypers
>> mailing list BangPypers at python.org 
>> http://mail.python.org/**mailman/listinfo/bangpypers<http://mail.python.org/mailman/listinfo/bangpypers>
>>
>
>> 
_______________________________________________
> BangPypers mailing list BangPypers at python.org 
> http://mail.python.org/mailman/listinfo/bangpypers
> 


- -- 
Regards,

- --Anand

-
--------------------------------------------------------------------------------------
Software Architect/Consultant
anandpillai at letterboxes.org

Please note my updated email address <anandpillai at letterboxes.org>.
Kindly update your address books.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBAgAGBQJSJO29AAoJEHKU2n17CpvDSK4IALaLNb/tP3MjUE6EvQskn8EQ
SLg2BuUY0dy2f+QfHQ2bBZLj3unuHHPHlugfK8wGbNpB80rGm0lpoyqxiU7H2JnN
aSXGyAS435VemqohOr26ZN/5nsYdSkz87KnJNlZ8YagPd+RaDZd/dc6BNM3er7va
DioJnm+txAqIenv3xGeYNQfs7U7QljKcdrzqPjJuQp65ZmSUbcfmPls1I7B6Oxxe
1Xj+ax/N3wJeNKkWZKsvOJsHgQNMjNhTICfH8Ofsv0c04mlXI/Ej3+yIzQ/2tZwc
k9jQrlX/Wgv+Mza41RPCtJTJGkbfFVTCOiQQjvwA4ZnriuwmTAcH2axELbSr1aU=
=E2cM
-----END PGP SIGNATURE-----


More information about the BangPypers mailing list