Program Termination

A program terminates when no more statements exist to execute in the input program, when an uncaught SystemExit exception is raised (as generated by sys.exit()), or when the interpreter receives a SIGTERM or SIGHUP signal (on UNIX). On exit, the interpreter decrements the reference count of all objects in all the currently known namespaces (and destroys each namespace as well). If the reference count of an object reaches zero, the object is destroyed and its __del__() method is invoked.

It’s important to note that in some cases the __del__() method might not be invoked at program termination. This can occur if circular references exist between objects (in which case objects may be allocated, but accessible from no known namespace). ...

Get Python: Essential Reference, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.