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

P

prouleau001

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::
... def goodbye():
... print "Goodbye, terminating..."
... Traceback (most recent call last):

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::
... atexit.register(fct)
... return fct
... ... def goodbye2():
... print "Goodbye 2!!"
...
... 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
 
S

Skip Montanaro

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
 
C

Carsten Haese

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

prouleau001

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

Georg Brandl

Skip said:
...

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top