By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,171 Members | 809 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,171 IT Pros & Developers. It's quick & easy.

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

P: n/a
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/li...le-atexit.html
... _Python 2.4 Decorators\: Reducing code duplication and consolidating
knowledge: http://www.ddj.com/184406073

Nov 16 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
Nov 16 '06 #2

P: n/a
On 11/16/06, Skip Montanaro <sk************@gmail.comwrote:
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...70&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.
Nov 16 '06 #3

P: n/a
On Thu, 2006-11-16 at 08:03 -0800, pr*********@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.

-Carsten
Nov 16 '06 #4

P: n/a


On Nov 16, 11:38 am, Carsten Haese <cars...@uniqsys.comwrote:
On Thu, 2006-11-16 at 08:03 -0800, prouleau...@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.

Nov 16 '06 #5

P: n/a
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
Nov 16 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.