Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from collections import namedtuple
from pympler.asizeof import asizeof
class PointSimple:
def __init__(self, x, y):
self.x = x
self.y = y
class PointNamedTuple(namedtuple('PointNamedTuple', ['x', 'y'])):
__slots__ = ()
@property
def hypot(self):
return (self.x ** 2 + self.y ** 2) ** 0.5
def __str__(self):
return f'Point: x={self.x} y={self.y} hypot={self.hypot}'
class PointNoSlots(namedtuple('PointNoSlots', ['x', 'y'])):
@property
def hypot(self):
return (self.x ** 2 + self.y ** 2) ** 0.5
def __str__(self):
return f'Point: x={self.x} y={self.y} hypot={self.hypot}'
classes = PointSimple, PointNamedTuple, PointNoSlots
for cls in classes:
print(f'class = {cls.__name__}; real size = {asizeof(cls(1, 2))}')
@tirinox

This comment has been minimized.

Copy link
Owner Author

@tirinox tirinox commented Feb 7, 2021

class = PointSimple; real size = 328
class = PointNamedTuple; real size = 120
class = PointNoSlots; real size = 128
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment