Atexit Module Introduction
Function: Let the registered function automatically execute when the interpreter terminates normally, can register several functions, the registered function will be executed in reverse order (according to the data, the reason of reverse is caused by function pressure stack, advanced after)
1, normal registration, such as below.
defGoodbye (name, adjective):Print("Goodbye%s, it is%s to meet."%(name, adjective))defHello ():Print('Hello world!')defA ():Print('a')ImportAtexitatexit.register (Goodbye,'Donny',' Nice') Atexit.register (a) hello ()#OutputPS e:\atom\files\app>python. \ex9_atexit.pyhello World!agoodbye Donny, it is nice to meet you.
2, you can use the adorner to register, but only applicable to no parameters when called.
Import Atexit@atexit.register def Hello (): Print ('Hello world! ' )# output PS e:\atom\files\app> python. \ex9_atexit.pyhello world!
3, cancel registration, such as below.
defGoodbye (name, adjective):Print("Goodbye%s, it is%s to meet."%(name, adjective))defHello ():Print('Hello world!')defA ():Print('a')ImportAtexitatexit.register (Goodbye,'Donny',' Nice') Atexit.register (a) atexit.register (a) atexit.register (a) Hello () atexit.unregister (a)#OutputPS e:\atom\files\app>python. \ex9_atexit.pyhello world! Goodbye Donny, it is nice to meet.
This module is typically used to clean up resources at the end of a program.
Python atexit Module