Does Python intern the elements of a list?

Viewed 118
num = [1, 2, 3]

As far as I know, the num list contains references to the int objects. Since the values are in the range [-5, 256], I feel that they should be cached. Am I correct in my assessment?

2 Answers
>>> lst = [1, 2, 3]
>>> 1 is lst[0]
True

Seems to be the case on my build (CPython 3.8).

The interning mechanism should not care about what references (variable names, list positions, ...) point to the object - they are unidirectional anyway.

Any list? No. Most objects are not cacheable.

This list? Yes, because they're integers in the range [-5, 256] like you said.

Keep in mind this is an implementation detail of CPython.

Related