In Python, if two variables reference to the same object, does the total size (memory allocation) double or is the total size just the size of the object?
As variables reference to objects, does that mean the same object is not created twice if it's referenced twice?
I tried to get the __sizeof__()
but I'm unsure how should I study this myself.
a = "string"
b = a
Now calling the __sizeof__()
method on the string referenced by a
will output 55 bytes in Jupyter notebook. B will do the same obviously because it references the same object.
But is the total size of the objects used here 1x or 2x size of object "string"? I don't really trust the file size of a .py or .ipynb on this one to get an accurate answer.
Edit: when does Python allocate new memory for identical strings? addresses only strings. The answer to this question depends partially on the type of the object (especially small integers).
Objects can be either 1) equal in value or 2) equal in object id, respectively ==
and is
checks. For equal in value, the two references might point to the same object or two different objects (of equal value). For equal in object id, the two references point only to one object.
In the first case the total size is two references + one or two objects depending on the implementation. For the second case, there's only two references and one object.
For small integers (and small strings), Python caches them automatically so they are always sharing a reference even if two object assignments are done. Both ==
and is
will evaluate to True
.
Same value, same object:
S1 = 'string'
S2 = 'string'
S1 == S2, S1 is S2
#Out: (True, True)
Same value, different object:
S1 = 'a longer string'
S2 = 'a longer string'
S1 == S2, S1 is S2
#Out: (True, False)