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

Discussion in 'Python' started by prouleau001@gmail.com, Nov 16, 2006.

  1. Guest

    Hi all!

    Since that the decorator syntax is upon us, I think it would be good if
    atexit.register() was returning the function passed as argument. This
    simple change to the library would solve a problem with the use of
    atexit.register as a decorator (and I can't think of any use case where
    this change would break any code).

    I describe the problem in the following text.

    Problem using atexit.register as a decorator
    ============================================

    In his April 2005 article titled `Python 2.4 Decorators: Reducing code
    duplication and consolidating knowledge`_ , Phillip Eby describes how
    you can use `atexit.register()`_ from the standard Python library. He
    shows how to use the decorator syntax to register a function that will
    execute at program termination. Here is how it goes::

    @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!
    You can see this in the following session::

    >>> import atexit
    >>> @atexit.register

    ... def goodbye():
    ... print "Goodbye, terminating..."
    ...
    >>> goodbye()

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: 'NoneType' object is not callable
    >>>
    >>> goodbye
    >>> type(goodbye)

    <type 'NoneType'>
    >>>


    There is two solutions to this problem:

    1. Use another function to register and decorate.
    2. Change atexit.register() in the Python library so that
    it returns the function it registers.

    Solution 1 can be implemented right away::

    def atexit_register(fct):
    atexit.register(fct)
    return fct

    @atexit_register
    def goodbye2():
    print "Goodbye 2!!"

    and it works: it registers the function for execution at termination
    but leaves goodbye2 callable::

    >>> def atexit_register(fct):

    ... atexit.register(fct)
    ... return fct
    ...
    >>> @atexit_register

    ... def goodbye2():
    ... print "Goodbye 2!!"
    ...
    >>> goodbye2()

    Goodbye 2!!
    >>> goodbye2

    <function goodbye2 at 0x009DD930>
    >>>


    ... References

    ... _atexit.register():
    http://www.python.org/doc/current/lib/module-atexit.html
    ... _Python 2.4 Decorators\: Reducing code duplication and consolidating
    knowledge: http://www.ddj.com/184406073
     
    , Nov 16, 2006
    #1
    1. Advertising

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

    > Since that the decorator syntax is upon us, I think it would be good if
    > atexit.register() was returning the function passed as argument. This
    > simple change to the library would solve a problem with the use of
    > atexit.register as a decorator (and I can't think of any use case where
    > this change would break any code).

    ....

    Can you submit a bug report to the SourceForge bug tracker? I'll take
    care of the problem when I have access to the subversion repository.

    Skip
     
    Skip Montanaro, Nov 16, 2006
    #2
    1. Advertising

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

    On 11/16/06, Skip Montanaro <> wrote:
    > > Since that the decorator syntax is upon us, I think it would be good if
    > > atexit.register() was returning the function passed as argument. This
    > > simple change to the library would solve a problem with the use of
    > > atexit.register as a decorator (and I can't think of any use case where
    > > this change would break any code).

    > ...
    >
    > Can you submit a bug report to the SourceForge bug tracker? I'll take
    > care of the problem when I have access to the subversion repository.
    >

    Done: http://sourceforge.net/tracker/index.php?func=detail&aid=1597824&group_id=5470&atid=105470

    I select Python 2.5 as the category. It affects all versions but most
    likely to cause a problem in Python 2.4 and after.

    --

    Pierre R.
     
    Pierre Rouleau, Nov 16, 2006
    #3
  4. Re: atexit.register does not return the registered function. IMHO,it should.

    On Thu, 2006-11-16 at 08:03 -0800, 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.

    -Carsten
     
    Carsten Haese, Nov 16, 2006
    #4
  5. Guest

    On Nov 16, 11:38 am, Carsten Haese <> wrote:
    > On Thu, 2006-11-16 at 08:03 -0800, 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.
     
    , Nov 16, 2006
    #5
  6. Georg Brandl Guest

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

    Skip Montanaro schrieb:
    >> Since that the decorator syntax is upon us, I think it would be good if
    >> atexit.register() was returning the function passed as argument. This
    >> simple change to the library would solve a problem with the use of
    >> atexit.register as a decorator (and I can't think of any use case where
    >> this change would break any code).

    > ...
    >
    > Can you submit a bug report to the SourceForge bug tracker? I'll take
    > care of the problem when I have access to the subversion repository.


    Sorry, didn't read this thread before the bug report, which is why I
    already handled this one ;)

    cheers,
    Georg
     
    Georg Brandl, Nov 16, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JV
    Replies:
    4
    Views:
    1,628
  2. Replies:
    7
    Views:
    4,174
    Wilfred Knievel
    Mar 13, 2009
  3. Cornelius
    Replies:
    2
    Views:
    327
  4. Daniel Carrera

    Ruby vs Python (IMHO)

    Daniel Carrera, Nov 29, 2003, in forum: Ruby
    Replies:
    9
    Views:
    266
    Bauduin Raphael
    Nov 30, 2003
  5. Andrea Crotti

    atexit.register in case of errors

    Andrea Crotti, Feb 15, 2012, in forum: Python
    Replies:
    7
    Views:
    160
    Miki Tebeka
    Feb 15, 2012
Loading...

Share This Page