The magic of "self": How Python inserts "self" into methods.
summary
Claim: Python inserts the instance by making plain functions into descriptors whose __get__ creates a bound method so the instance becomes the first arg. Tools shown: the descriptor protocol (implementing __get__), a handwritten FavoriteDescriptor that compares an instance.name to "warwick streamer", and demos of property, classmethod, staticmethod. The descriptor demo returns True/False as expected and the manual invocation of get(None) vs get(instance) reproduces the exact behavior that produces automatic self binding.
AI-written summary · about
chapters (9)
- 0:00Introduction
- 1:50About me
- 3:17The basics
- 5:53Defining a class with methods
- 10:33What are descriptors
- 12:32Implementing a descriptor
- 18:18decorators
- 20:14Summary
- 22:14Conclusion
related talks
26mThe Magic of Self How Python inserts self into methods
26mThe Magic of Self: How Python Inserts self Into Methods
45mDescriptors made easy
30mThe Wizardry of Metaprogramming
38mUnderstanding attributes (Or: They're not nearly as boring as you think!)
47mA Deep-Dive Into Python Classes
31mLife Under the A Guide to Python's Internal Conventions
25mThe Magic of Python
