When are python classes and class attributes garbage collected?

PythonServer Side ProgrammingProgramming

A class attribute exists until the last reference goes away. A global variable also exists until the last reference goes away. Neither of these are guaranteed to last the entire duration of the program.

Also, a class defined at module scope is a global variable. So the class (and, by implication, the attribute) have the same lifetime as a global variable in that case. If no instances of the class are currently live, the class and its class attributes might be garbage-collected if their reference counts become zero.

Published on 29-Jan-2018 10:13:33