07_Classes_and_the_Data_Model — every operator is a method, and every lookup is a search¶
Level: 201 → 301 · for Python programmers, especially ones coming from ABAP, Java or C#
A class in Python is made at run time by running its body, and an object of that class is a namespace whose lookups fall through to the class. Almost nothing else is built in. ==, hash(), len(), if x, obj.x, with, for and + each become a call to a method with a double-underscored name, which the class may define, inherit or leave to object's defaults. The pages here are the places where that design gives an answer a reader from a language with declared fields, a static keyword and a fixed equals-and-hashCode pair does not expect.
| # | Lesson | The question it answers | Status |
|---|---|---|---|
| 1 | A class attribute is shared | Why did every instance see the item I appended through one — and why did c.count = 1 not change Counter.count? |
written |
| 2 | obj.x is a search |
Why did a @property beat the instance attribute — and why does __getattr__ not run for names that exist? |
stub |
| 3 | super() is not the parent |
Why did super().__init__() call a class that is not my base class? |
stub |
| 4 | class is a call |
What actually runs when Python reaches a class statement — and when do I need a metaclass? |
stub |
| 5 | Defining __eq__ deletes __hash__ |
Why did my class stop working as a dict key the moment I gave it __eq__ — and why is my @dataclass unhashable? |
written |
| 6 | if x calls a method |
Why is an object of my class always true — and why did if result: miss a legitimate 0? |
stub |
| 7 | @dataclass writes the methods |
Which methods did @dataclass write, which did it refuse, and what does it never check? |
stub |
Where this sits relative to the other libraries¶
Rust has no classes, and the structs ↗ and traits ↗ chapters of the Rust library are what it has instead: Comparison traits ↗ is page 5's pair of methods as two derives, Method resolution ↗ is page 2's search done by the compiler, and the absence of inheritance is page 3's whole subject seen from the other side. ABAP declares at the definition what Python decides by lookup: CLASS-DATA against DATA is page 1, and a hashed table's declared key is page 5. The concurrency library ↗ owns what happens when two threads touch one class attribute. Chapter 4's is is not == and hash() is not stable across runs are page 5's two methods seen from the built-in types.