0

I was trying to understand use of slots in Python, so I created two classes, one with slots and one without it, then, I created about a million objects and print the size of both the objects:

>>> class Foo(object): pass
>>> class Bar(object): __slots__ = ()
>>> foos = [Foo() for f in xrange(1000000)]
>>> bars = [Bar() for b in xrange(1000000)]
>>> import sys
>>> sys.getsizeof(foos)
4348732
>>> sys.getsizeof(bars)
4348732

Surprisingly both the objects are of same size. So now I am confused why is it said that slots save memory ?

P.S. I tried this on Linux, Python 2.7

This is not duplicate of: Usage of __slots__?

Here I want to know why the memory consumption in both the scenarios is same?

Community
  • 1
  • 1
openstk
  • 137
  • 9
  • 3
    You are comparing sizes of lists that only contain references to instances. Try `sys.getsizeof(bars[0])` and `sys.getsizeof(foos[0])` instead. – vaultah Jan 27 '17 at 08:02
  • Another duplicate: http://stackoverflow.com/q/30047388/2301450 – vaultah Jan 27 '17 at 08:07

0 Answers0