3 ms·
This is the correct answer for this if you're using Python and have lots of small objects with fixed attributes. In [1]: class A: ...: __slots__ = ('a
by orf 8y ago
This is the correct answer for this if you're using Python and have lots of small objects with fixed attributes.
In [1]: class A:
...: __slots__ = ('a', 'b')
...: def __init__(self):
...: self.a = 1
...: self.b = 2
...:
In [3]: get_size(A())
Out[3]: 112
In [4]: get_size((1,1))
Out[4]: 120
Smaller than a tuple. Not to say that 112 bytes is great, but __slots__ can really help with memory usage. Without:
In [23]: get_size(A())
Out[23]: 324
A named tuple, while not mutable, is 92 bytes with those two attributes.