By default, Python stores instance attributes in a dictionary (__dict__), which consumes extra memory. For thousands of instances, __slots__ can drastically reduce memory usage:
class Point:
__slots__ = ('x', 'y') # No __dict__ per instance
def __init__(self, x, y):
self.x = x
self.y = y
Benefits:
Drawback:
class Multiplier:
def __init__(self, factor):
self.factor = factor
def __call__(self, value):
return value * self.factor
double = Multiplier(2)
print(double(5)) # Output: 10
Use @abstractmethod with @classmethod, @staticmethod, @property. python 3 deep dive part 4 oop