I recently explained a friend the usage of __slots__
. I wanted to demonstrate the result to him and used the following code:
import sys
class Foo:
__slots__ = 'a', 'b'
def __init__(self, a, b):
self.a = a
self.b = b
class Bar:
def __init__(self, a, b):
self.a = a
self.b = b
a = Foo(10, 20)
b = Bar(10, 20)
print(sys.getsizeof(a))
print(sys.getsizeof(b))
The output on the console for Python 3 was:
56
56
The output for Python 2 was:
72
72
Why is there no difference in size?