[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:35:27 CEST 2013
On Tue, Sep 3, 2013 at 8:47 AM, Anand B Pillai
<anandpillai at letterboxes.org>wrote:
> -----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.
>
I'm with Anand C on this one, the context manager serves no real purpose
here; the initialization of sys.exitfunc is entirely incidental and has
little bearing on the fact that it is being initialized from the value
returned by the context manager. This seems to me even more un-idiomatic
than using atexit.register as a decorator. YMMV, but this is more like
shoehorning in a 'cute' language feature just for the heck of it.
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
>
This just conflates context/resource management with registering cleanup
functions, which seems entirely unnecessary.
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.
>
That would only hold if there's nothing following the with block in my
thread of execution. In any other case the cleanup function will be
executed much later and encapsulating it inside the context manager only
elides the intent of the code and makes it harder to read. Even if this
were just a single-file standalone script, I would much rather explicitly
serialize the code. I honestly fail to see the modularity in this idiom.
>
> > 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-----
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>
Also, aside from the point about python idioms, most of this discussion is
rendered moot by the fact that
sys.exitfunc<http://docs.python.org/2/library/sys.html#sys.exitfunc>has
been flagged as deprecated in favour of atexit.register since 2.4 and
suggesting it to a newbie just feels inappropriate.
- d
More information about the BangPypers
mailing list