Monday, April 19, 2010

Weekly Python module: atexit

New idea about few posts. What about smalltalk about one Python module in a week?

Lets go!



For this week Python standard module 'atexit'. Whole purpose of this module is to provide functions that will be executed at normal interpreter exit.

It has only one function register, that takes function and make sure that every function that was registered will be called in last in, first out order. If you know 'atexit()' C routine you will be familiar with it.

Small example:

def goodbye(name, adjective):
    print 'Goodbye, %s, it was %s to meet you.' % (name, adjective)

import atexit
atexit.register(goodbye, 'Donny', 'nice')

# or:
atexit.register(goodbye, adjective='nice', name='Donny')

register function can also be used as decorator:

import atexit

@atexit.register
def goodbye():
    print "You are now leaving the Python sector."

No comments:

Post a Comment