Metaprogramming with Decorators, Metaclasses, and Dynamic Code Generation
summary
Covers concrete patterns and working code: a logger decorator that wraps functions, a class-based counter decorator that maintains call counts via __call__, a custom metaclass using type with __new__ and __init__ to enforce required attributes and log class creation, and an exec-generated function example that creates a square function at runtime. Demonstrates where these tools power Django, Flask, and FastAPI, shows the examples running, and warns about unsafe exec/eval with remedies like ast.literal_eval and restricted namespaces.
AI-written summary · about
chapters (10)
- 0:00Metaprogramming in Python
- 3:39Python metaprogramming
- 9:24Function decorators
- 14:01Class decorators
- 16:53Decorators in frameworks
- 17:14Metaclasses in Python
- 21:30Custom metaclasses
- 24:19Metaclasses enforcing contracts
- 26:28Dynamic code generation
- 27:50Metaprogramming security
related talks
33mMetaprogramming in Python
02h 15mAll about decorators
30mThe Wizardry of Metaprogramming
03h 21mIntroduction to Decorators: Power Up Your Python Code
02h 21mIntroduction to Decorators: Power Up Your Python Code
29mOn the Use and Misuse of Decorators
29mHow Decorators Function
02h 32mIntroduction to Decorators: Power UP Your Python Code
