Skip to content

Instantly share code, notes, and snippets.

@tirinox
Created February 7, 2021 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tirinox/eb6aee9397cb5a90051cd1b749028210 to your computer and use it in GitHub Desktop.
Save tirinox/eb6aee9397cb5a90051cd1b749028210 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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