3

What's the lifetime of a class attribute, in Python? If no instances of the class are currently live, might the class and its class attributes be garbage-collected, and then created anew when the class is next used?

For example, consider something like:

class C(object):
    l = []

    def append(self, x):
        l.append(x)

Suppose I create an instance of C, append 5 to C.l, and then that instance of C is no longer referenced and can be garbage-collected. Later, I create another instance of C and read the value of C.l. Am I guaranteed C.l will hold [5]? Or is it possible that the class itself and its class attributes might get garbage-collected, and then C.l = [] executed a second time later?

Or, to put it another way: Is the lifetime of a class attribute "forever"? Does a class attribute have the same lifetime as a global variable?

D.W.
  • 3,382
  • 7
  • 44
  • 110

2 Answers2

4

You asked several questions.

What's the lifetime of a class attribute, in Python?

A class attribute lives as long as there is a reference to it. Since the class holds a reference, it will live as at least long as the class lives, assuming that the class continues to hold the reference. Additionally, since each object holds a reference, it will live at least as long as all of the objects, assuming that each object continues to hold the reference.

Am I guaranteed C.l will hold [5]?

In the hypothetical that you describe, yes.

Or is it possible that the class itself and its class attributes might get garbage-collected, and then C.l = [] executed a second time later?

Not given your hypothetical that you are able to construct an instance of C. If you are able to construct a second instance of C, then C must exist, and so too must C.l

Is the lifetime of a class attribute "forever"?

No. The lifetime of a class attribute follows the lifetime rules of any object. It exists as long a reference to it exists. In the case of C.l, a reference exists in the class, and a reference exists in each instance. If you destroy all of those, then C.l will also be destroyed.

Does a class attribute have the same lifetime as a global variable?

Sort of: 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.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
2

If no instances of the class are currently live, might the class and its class attributes be garbage-collected, and then created anew when the class is next used?

No. There is no "next use" of a class that's been garbage-collected, because the only way it can be garbage-collected is if there's no way left to use it.

Suppose I create an instance of C, append 5 to C.l, and then that instance of C is no longer referenced and can be garbage-collected. Later, I create another instance of C and read the value of C.l. Am I guaranteed C.l will hold [5]?

Yes. The class has a live reference because you created another instance, and the list has a live reference as an attribute on the class.

Does a class attribute have the same lifetime as a global variable?

If the class holds a reference to something, that something will live at least as long as the class.

Ry-
  • 218,210
  • 55
  • 464
  • 476