'Empty class size in python
I just trying to know the rationale behind the empty class size in python, In C++ as everyone knows the size of empty class will always shows 1 byte(as far as i have seen) this let the run time to create unique object,and i trying to find out what size of empty class in python:
class Empty:pass # i hope this will create empty class
and when i do
import sys
print ("show",sys.getsizeof(Empty)) # i get 1016
I wonder why the Empty
takes this much 1016(bytes)? and also does the value(1016) it returns is this a standard value that never change(mostly) like C++?, Do we expect any zero base class optimization from python interpreter?Is there any way we can reduce the size of am Empty(just for curiosity sake)?
Solution 1:[1]
I assume you are running a 64 bit version of Python 3. On 32 bit Python 3.6 (on Linux), your code prints show 508
.
However, that's the size of the class object itself, which inherits quite a lot of things from the base object
class. If you instead get the size of an instance of your class the result is much smaller. On my system,
import sys
class Empty(object):
pass
print("show", sys.getsizeof(Empty()))
output
show 28
which is a lot more compact. :)
FWIW, on Python 2.6.6, sys.getsizeof(Empty)
returns 448 for a new-style class, and a measly 44 for an old-style class (one that doesn't inherit from object
). sys.getsizeof(Empty())
returns 28 for a new-style class instance and 32 for an old-style.
You can reduce the size of an instance by using __slots__
This class variable can be assigned a string, iterable, or sequence of strings with variable names used by instances.
__slots__
reserves space for the declared variables and prevents the automatic creation of__dict__
and__weakref__
for each instance.
import sys
class Empty(object):
__slots__ = []
print("show", sys.getsizeof(Empty()))
output
show 8
Please read the docs to understand how to use this feature.
Solution 2:[2]
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.
Solution 3:[3]
Choose Cpp or Delphi and you will get 4 bytes of the class inherited from TObject. Customer Guide - products based on Python and Java should not be implemented if you are limited in hardware capacity, this is obviously a losing solution.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | advance512 |
Solution 3 | Ari Cooper-Davis |