In Python, classes are not just blueprints; they are first-class objects. Because they are objects, something must create them. That "something" is a metaclass. By default, the metaclass for all Python objects is type . Understanding type as a Callable
from abc import ABCMeta
class Rectangle(Shape): def __init__(self, width, height): self.width = width self.height = height python 3 deep dive part 4 oop
This is extremely powerful, allowing you to automatically register subclasses, transform attributes, or enforce coding standards.
def deposit(self, amount): self.__balance += amount In Python, classes are not just blueprints; they
def area(self): return self.width * self.height
__repr__ : Ideally, a string that looks like a valid Python expression used to reconstruct the object (for developers). __str__ : A readable string representation for the user. Comparison Operators By default, the metaclass for all Python objects is type
By default, Python instances store attributes in a per-object dictionary ( __dict__ ). While this provides exceptional flexibility (you can add attributes at runtime), it comes with a memory cost. Each dictionary has its own memory overhead, which can be significant when you create millions of small objects.
: Understanding the relationship between class objects and instance objects, including class-level data and function attributes. Methods and Binding
class Shape(ABC): @abstractmethod def area(self): """Calculate the area. Must implement.""" pass