In Python, everything (literal, list, function) is an object:
isinstance(1,object) # True
isinstance(list(),object) # True
Each object contains 3 pieces of data:
Reference Count is used for memory management and tells you the reference count of an object. It is increased or decreased when a pointer to the object is copied or deleted; when the reference count reaches zero there are no references to the object left and it can be removed from the heap.
We can use the sys.getrefcount() method to get the reference count:
import sys
print(sys.getrefcount(7)) # 23
In the above example, we see that there are 23 things in Python that have a value of 7: since integers are an immutable data type, Python can save computing resources by having all the variables that contain 7 refer to the same data in the computer’s memory.
If we now assign 7 to a new variable: We didn’t dedicate any new memory to storing the value 7; we just created a new reference to the same memory that already contained 7.
foo = 7
print(sys.getrefcount(7)) # 24