Python 2.7.11 (default, Apr 12 2016, 14:09:35)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
>>> class Empty:pass
>>> import sys
>>> print ("show",sys.getsizeof(Empty)) # i get 1016
('show', 104)
>>> print ("show",sys.getsizeof(Empty()))
('show', 72)
Not 1016 bytes. As you can see, this is implementation specific. Other interesting sizes, using new-style classes:
>>> print ("show",sys.getsizeof(object()))
('show', 16)
>>> class Empty2(object):pass
>>> print ("show",sys.getsizeof(Empty2))
('show', 904)
>>> print ("show",sys.getsizeof(Empty2()))
('show', 64)
An empty class definition is still a class definition and hence a class object (more here):
When a class definition is entered, a new namespace is created, and used as the local scope ..
When a class definition is left normally (via the end), a class object is created. This is basically a wrapper around the contents of
the namespace created by the class definition ...
All class objects have a minimal set of attributes, which you see by doing:
>>> dir(Empty)
['__doc__', '__module__']
>>> dir(Empty2)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
And also other implementation details, which is the reason for their larger than naively-expected size.
Finally, note that that the class instances are indeed rather small, as expected, and not as big as class objects. Empty is a class object. Empty() is a class instance.
104on my machine.104if you are on Python2.xobjectwhich already comes with some baggage. Look atdir(Empty), it's not an empty list!