Python atexit Module

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.