When are python classes and class attributes garbage collected?


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.

Updated on: 30-Jul-2019

303 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements