What kind of data does the Python interpeter store for an object?
For example, in code like the following:
class MyClass:
pass
if __name__ == "__main__":
c = MyClass()
import sys
print sys.getsizeof(c),sys.getsizeof(MyClass)
Why is the output 72 and 104? Why is the class bigger than the object instance? What do the class and the object have to store that takes up 72 characters and 104 characters?
Surprisingly, when I run this:
class MyClass:
def __init__(self):
self.mIntValue = 1024
self.mStringValue = "hust";
if __name__ == "__main__":
c = MyClass()
import sys
print sys.getsizeof(c),sys.getsizeof(MyClass)
The output is still 72 and 104, but I added two extra properties, so I guess that the object should become "bigger". Well, the result seems not so.
cand 488 forMyClass.MyClassis an old-style class. Those should be avoided, and I wouldn't be surprised if that somehow accounts for most of those 72 bytes.getsizeofdoesn't include the size of__dict__.PyObjectandPyTypeObject). Also,sizeofworks like in C: it does not "follow" pointers (aconst char *is always the size of a pointer, even if it points to a 1Gb memory space).