Learning Objectives: After this lesson, you'll understand the difference between
__new__and__init__, build metaclasses for class-level customization, use ABC for abstract classes, and master__init_subclass__for simpler class configuration.
Python's Object Model
Before diving into metaclasses, let's visualize Python's fundamental type hierarchy:
Key insight: Classes are objects too! They're instances of type (or a custom metaclass).
Understanding Object Creation
Before metaclasses, let's understand how Python creates objects.
__new__ vs __init__
Controlling Instance Creation with __new__
__new__ for Factory Patterns
Metaclasses: Classes of Classes
A metaclass is the class of a class. Just as objects are instances of classes, classes are instances of metaclasses.
The type() Function
Building a Metaclass
Practical Metaclass: Auto-Registration
Metaclass: Attribute Validation
Abstract Base Classes (ABC)
ABCs provide a way to define interfaces and ensure subclasses implement required methods.
Abstract Properties
__init_subclass__: Modern Class Customization
Python 3.6+ provides __init_subclass__ as a simpler alternative to metaclasses.
Basic init_subclass
Validation with init_subclass
Combining init_subclass with Decorators
Class Decorators vs Metaclasses
Practice Exercises
Exercise 1: Enum-like Metaclass
Exercise 2: Field Descriptor with Metaclass
Key Takeaways
| Concept | Description |
|---|---|
__new__ | Creates and returns the instance |
__init__ | Initializes the instance |
| Metaclass | Class of a class, controls class creation |
type() | Built-in metaclass, creates classes dynamically |
| ABC | Abstract Base Class for interfaces |
__init_subclass__ | Hook for customizing subclass creation |
When to Use Each Approach
| Need | Solution |
|---|---|
| Control instance creation | __new__ |
| Simple interface enforcement | ABC with @abstractmethod |
| Register subclasses | __init_subclass__ |
| Modify class after creation | Class decorator |
| Full control over class creation | Metaclass |
| Validate class definition | Metaclass or __init_subclass__ |
Next Steps
In the next lesson, we'll explore Threading and the GIL—understand concurrent Python, the Global Interpreter Lock, synchronization primitives, and when threading helps vs. hurts.
Ready for concurrent Python? Threading awaits!