Python Language Metaclasses

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Introduction

Metaclasses allow you to deeply modify the behaviour of Python classes (in terms of how they're defined, instantiated, accessed, and more) by replacing the type metaclass that new classes use by default.

Remarks

When designing your architecture, consider that many things which can be accomplished with metaclasses can also be accomplished using more simple semantics:

  • Traditional inheritance is often more than enough.
  • Class decorators can mix-in functionality into a classes on a ad-hoc approach.
  • Python 3.6 introduces __init_subclass__() which allows a class to partake in the creation of its subclass.


Got any Python Language Question?