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

Saager Mhatre saager.mhatre at gmail.com
Sun Sep 8 19:13:37 CEST 2013


On Tue, Sep 3, 2013 at 1:27 AM, Anand B Pillai
<anandpillai at letterboxes.org>wrote:

> -----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
>

With a verb name like *register* I'd much prefer to call the function as
opposed to using it as a decorator. The more idiomatic decorators have noun
names, viz- property, classmethod, staticmethod, unittest.expectedFailure,
(or the one you used below) contextmanager, etc. I'll admit
'unittest.skipTest' is an anomaly, but I've always felt that the unittest
module was a somewhat unpythonic 1-1 port of JUnit, so we'll let that one
slide. :)

The other reason in favour of the invocation syntax over the decorator
syntax would be that the 'atexit.register' function is variadic and it
would not be possible to send in the arguments to be passed in at exit if
'atexit.register' were used as a decorator. Of course, we could nest the
decorated function in another function to provide a lexical closure that
would then allow us to specify those arguments, but explicitly declaring
the parameters to the cleanup function and explicitly passing them to
register just seems much more idiomatic to me.


> [...]
> - --
> 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-----
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>

I realize I'm a little late to the thread, but just thought I'd put in my
2c.

- d


More information about the BangPypers mailing list