The Magic of Python
summary
Introduces Python’s special (magic/dunder) methods and shows concrete examples for using them. Covers object initialization and representation (__init__, __repr__/__str__), numeric emulation (__add__ and similar), sequence emulation and iteration (__iter__, __getitem__), context managers (__enter__/__exit__), and using abc.AbstractBaseClass to gain functionality by implementing these methods.
AI-written summary · about
chapters (12)
- 0:00Intro
- 1:09Agenda
- 1:30Magic Methods?
- 4:20Define Custom Behavior
- 7:25Enable Operator Overloading
- 12:16Emulate Container Types
- 14:56getitem__
- 16:53Use Case: Checking for interfaces
- 18:01Use Case: Offer Free Mixin Methods
- 19:53Context Managers
- 23:53@contextlib.contextmanager
- 24:42+ Python Data Model Documentation → Python collections module documentation → A guide to Python's Magic Methods
related talks
01h 18mPython 103: Mmmm... Understanding Python's Memory Model, Mutability, and Methods
22mLearning Parseltongue: Wizardry in Python
01h 18mPython 103: Mmmm... Understanding Python's Memory Model, Mutability, and Methods
26mThe Magic of Self: How Python Inserts self Into Methods
46mOn Being Pythonic
01h 10mIt's Pythons All The Way Down: Python Types & Metaclasses Made Simple
30mThe Wizardry of Metaprogramming
31mPython Data Model
