python标准库介绍——11 atexit 模块详解

=== atexit 模块===


(用于2.0版本及以上) ``atexit`` 模块允许你注册一个或多个终止函数(暂且这么叫), 
这些函数将在解释器终止前被自动调用.

调用 ``register`` 函数, 便可以将函数注册为终止函数, 如 [Example 1-78 #eg-1-78] 所示. 
你也可以添加更多的参数, 这些将作为 ``exit`` 函数的参数传递. 

====Example 1-78. 使用 atexit 模块====[eg-1-78]

```
File: atexit-example-1.py

import atexit

def exit(*args):
    print "exit", args

# register two exit handler
atexit.register(exit)
atexit.register(exit, 1)
atexit.register(exit, "hello", "world")

*B*exit ('hello', 'world')
exit (1,)
exit ()*b*
```

该模块其实是一个对 ``sys.exitfunc`` 钩子( hook )的简单封装. 

 

原文地址:https://www.cnblogs.com/xuchunlin/p/7748322.html