atexit.register does not return the registered function. IMHO, it should.

prouleau001 at gmail.com prouleau001 at gmail.com
Thu Nov 16 11:58:42 EST 2006



On Nov 16, 11:38 am, Carsten Haese <cars... at uniqsys.com> wrote:
> On Thu, 2006-11-16 at 08:03 -0800, prouleau... at gmail.com wrote:
> >   @atexit.register
> >   def goodbye():
> >       print "Goodbye, terminating..."
>
> > However, there is one fundamental problem with this: atexit.register()
> > returns None. Since the above code corresponds to::
>
> >   def goodbye():
> >       print "Goodbye, terminating..."
> >   goodbye = atexit.register(goodbye)
>
> > the code registers goodbye but right after it binds goodbye to None!While it wouldn't hurt to have atexit.register return the function it
> registered, this "problem" is only a problem if you wish to call the
> function manually, since atexit already registered the reference to the
> intended function before your reference to it gets rebound to None.
> Normally one would register a function with atexit precisely because
> they don't want to call it manually.
>
There are *two* problems.

1 - As you said, most of the time you would not call the function
explicitly, but in some situation you might want to.
2-  If you want to document your code using introspection, or use an
IDE to look at the function, if the function disappeared from sight,
you won't be able to.


The second problem is similar to what happens when a decorator changes
the signature of a function.

--

P.R.




More information about the Python-list mailing list