This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))}') |
Author
tirinox
commented
Feb 7, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment