[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
Tue Sep 3 05:17:19 CEST 2013


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

On Tuesday 03 September 2013 06:27 AM, Anand Chitipothu wrote:
>> 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.


Well, nothing really except maybe,

@contextmanager
def end_of_days():
    before_context()
    def wrapper():
        print 'Goodbye.'
    yield wrapper
    print after_context()

with end_of_days() as sys.exitfunc:
    print 3+4

Just provides a context manager as well to do things
before entering context and after exiting (just before
sys.exit in this case), in a modular way.

> 
> Anand _______________________________________________ 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/

iQEcBAEBAgAGBQJSJVS7AAoJEHKU2n17CpvDbdsIAJD8T8AJp49jmE5l/tEpUCKu
k5U0e3cTcy7rAevxhE40lFjvmTAENw6K85Eh1hTbVt0uRQDhdDs7Lv47WuVHEppH
28WlGkufwMHc5fIPb1XVbL7UbzPcqZd/6tUTXN+AAYZYtx2OB1rRiG6REhiFyY+Z
+8hPwMWcs6n3oV8hCJbdFITaRM9pm4RozBolFVe+r5LWBp1IO56fSt0IFDS9gceI
5NhLI9JZReoqYEfK8cPtNBjM9y1tLGJ8AZP60E0n3WGgVZMFK0Mw1xloefIZvaBC
xa5l81BVq+zov50WkXTQ/FVCGQpJM7l9A9XRfTznC5nnx2QVbitOPgFNNZpIYQc=
=V14p
-----END PGP SIGNATURE-----


More information about the BangPypers mailing list