1

Note: Please go through my question completely before marking it duplicate or irrelevant, Please ! and also, Please forgive me if it is a silly question ...!

I created two classes one with slots and one without it, as follows:

import guppy

class A(object):

    def __init__(self):
        self.a = 1
        self.b = 2
        self.c = 3


class B(object):
    __slots__ = ['a', 'b', 'c']

    def __init__(self):
        self.a = 1
        self.b = 2
        self.c = 3

a = [A() for i in xrange(1000000)]
b = [B() for i in xrange(1000000)]

print guppy.hpy().heap()

At the end of the program, I am printing the memory cunsumtion of Python heap. To my surprise a program with slots is taking more memory. Here is the output of guppy.hpy().heap():

enter image description here

P.S: I even tried with slots = ('a', 'b', 'c'), result is still the same

openstk
  • 137
  • 9
  • 3
    Note that for instances of type A you need to include the contribution of A's `__dict__` (the first, unhighlighted, line in your guppy output). – Mark Dickinson Feb 02 '17 at 07:22

0 Answers0