Understanding attributes (Or: They're not nearly as boring as you think!)
summary
Explains exactly what the dot does and why attributes drive Python behavior with concrete examples: variables vs object attributes, a population counter moved from a broken global into a class attribute, class bodies executing at definition time (demo prints order a b d e c c), how __init__ actually adds attributes, and how inheritance requires calling super to run parent __init__. Dives into ICPO (instance class parent object) lookup, the descriptor __get__ protocol and how it produces bound methods via functools.partial, and recommends properties as simpler descriptors.
AI-written summary · about
chapters (37)
- 0:00Intro
- 0:47I teach Python!
- 1:11Let's assign to a variable!
- 2:12Let's get fancier
- 2:57Variables vs. attributes
- 3:24Reading from attributes
- 4:29Setting attributes
- 5:40We set attributes all the time
- 7:28What's missing from this program?
- 8:11Idea: A global variable
- 10:28A better alternative
- 11:15This is pretty ugly
- 11:35What will this code print?
- 11:57The answer
- 14:08Methods are class attributes
- 14:37Classes are file-less modules
- 15:25Class attribute, nicer edition
- 15:57Static variables.!?
- 17:47Let's walk through this
- 18:48A new version of Person
- 19:36We need a new class
- 20:41Does it work?
- 21:14Try again, but with inheritance
- 21:58Let's use inheritance
- 22:09Better use of inheritance
- 23:48The problem?
- 24:33Solution: Explicitly call Person.init
- 24:46Better: Use super
- 25:27Printing our objects
- 26:23Operator overloading
- 26:41Something is still missing
- 28:44Example
- 29:41Use it in a class attribute
- 31:09Retrieve the value
- 31:54What are gets parameters?
- 33:25Methods are descriptors
- 35:37Where is our original function?
related talks
47mA Deep-Dive Into Python Classes
31mLife Under the A Guide to Python's Internal Conventions
01h 18mPython 103: Mmmm... Understanding Python's Memory Model, Mutability, and Methods
26mThe Magic of Self: How Python Inserts self Into Methods
27mVariables and objects in Python: it's pointers all the way down
45mDescriptors made easy
01h 10mIt's Pythons All The Way Down: Python Types & Metaclasses Made Simple
19mObjects and classes in Python (and JavaScript)
