惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threatpost
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Blog of Author Tim Ferriss
S
SegmentFault 最新的问题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 司徒正美
T
Tailwind CSS Blog
The Cloudflare Blog
The Last Watchdog
The Last Watchdog
PCI Perspectives
PCI Perspectives
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
TaoSecurity Blog
TaoSecurity Blog
云风的 BLOG
云风的 BLOG
C
Cybersecurity and Infrastructure Security Agency CISA
O
OpenAI News
Recorded Future
Recorded Future
GbyAI
GbyAI
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Y
Y Combinator Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
量子位
博客园 - 叶小钗
V
Vulnerabilities – Threatpost
F
Full Disclosure
Recent Announcements
Recent Announcements
Vercel News
Vercel News
S
Schneier on Security
H
Heimdal Security Blog
Cisco Talos Blog
Cisco Talos Blog
V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
B
Blog RSS Feed
宝玉的分享
宝玉的分享
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Privacy & Cybersecurity Law Blog
T
Threat Research - Cisco Blogs
G
Google Developers Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
爱范儿
爱范儿
IT之家
IT之家
大猫的无限游戏
大猫的无限游戏
C
Check Point Blog
N
Netflix TechBlog - Medium
S
Security @ Cisco Blogs
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Microsoft Azure Blog
Microsoft Azure Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cyberwarzone
Cyberwarzone

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
Python's Data Model Is an API. Here Is How to Use It Properly
shayan holak · 2026-04-29 · via DEV Community
<p>Most Python developers know that <code>__len__</code> makes <code>len()</code> work and <code>__add__</code> makes <code>+</code> work. That is the surface. The actual story is that Python's data model is a coherent, documented protocol through which user-defined objects can participate in the language itself: not just operators, but truthiness, hashing, iteration, context management, attribute access, memory layout, and more. Using it well means understanding what CPython actually calls, in what order, and why.</p> <h2> The Interpreter Calls These, Not You </h2> <p>The first thing to internalize: dunder methods are called by the interpreter, not by user code. When you write <code>len(obj)</code>, Python calls <code>type(obj).__len__(obj)</code>. Not <code>obj.__len__()</code>. The lookup goes through the type, not the instance.</p> <p>This matters for two reasons.</p> <p>First, defining a dunder on an instance rather than the class does not work:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="k">class</span> <span class="nc">MyClass</span><span class="p">:</span> <span class="k">pass</span> <span class="n">obj</span> <span class="o">=</span> <span class="nc">MyClass</span><span class="p">()</span> <span class="n">obj</span><span class="p">.</span><span class="n">__len__</span> <span class="o">=</span> <span class="k">lambda</span><span class="p">:</span> <span class="mi">42</span> <span class="nf">len</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span> <span class="c1"># TypeError: object of type 'MyClass' has no len() </span></code></pre> </div> <p>The interpreter checked <code>type(obj).__len__</code>, found nothing, and raised. The instance attribute was ignored entirely.</p> <p>Second, it means metaclasses can define dunders that apply to the class itself as an object. <code>__len__</code> on a metaclass makes <code>len(MyClass)</code> work. <code>__iter__</code> on a metaclass makes <code>for x in MyClass</code> work. The class is an instance of the metaclass, so the metaclass's dunders are the class's dunders.</p> <h2> Truthiness, Equality, and Hashing Are a Triangle </h2> <p>These three are deeply connected and breaking the contract between them is one of the more common ways to introduce subtle bugs.</p> <p><strong>Truthiness</strong> is determined by <code>__bool__</code>. If not defined, Python falls back to <code>__len__</code>: an object with length zero is falsy. If neither is defined, the object is always truthy.<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="k">class</span> <span class="nc">Container</span><span class="p">:</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="n">self</span><span class="p">,</span> <span class="n">items</span><span class="p">):</span> <span class="n">self</span><span class="p">.</span><span class="n">items</span> <span class="o">=</span> <span class="n">items</span> <span class="k">def</span> <span class="nf">__len__</span><span class="p">(</span><span class="n">self</span><span class="p">):</span> <span class="k">return</span> <span class="nf">len</span><span class="p">(</span><span class="n">self</span><span class="p">.</span><span class="n">items</span><span class="p">)</span> <span class="n">c</span> <span class="o">=</span> <span class="nc">Container</span><span class="p">([])</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">c</span><span class="p">:</span> <span class="nf">print</span><span class="p">(</span><span class="sh">"</span><span class="s">empty</span><span class="sh">"</span><span class="p">)</span> <span class="c1"># prints, because __bool__ fell back to __len__ </span></code></pre> </div> <p><strong>Equality</strong> is <code>__eq__</code>. The default is identity comparison (same as <code>is</code>). Override it to define value equality.</p> <p><strong>Hashing</strong> is <code>__hash__</code>. Here is the contract: objects that compare equal must have the same hash. If you define <code>__eq__</code>, Python automatically sets <code>__hash__</code> to <code>None</code>, making your object unhashable. This is intentional. Mutable objects with value equality should not be hashable because their hash could change when they are mutated, silently breaking set and dict behavior.<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="k">class</span> <span class="nc">Point</span><span class="p">:</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="n">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span> <span class="n">self</span><span class="p">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">x</span> <span class="n">self</span><span class="p">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">y</span> <span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="n">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span> <span class="k">return</span> <span class="nf">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">Point</span><span class="p">)</span> <span class="ow">and</span> <span class="n">self</span><span class="p">.</span><span class="n">x</span> <span class="o">==</span> <span class="n">other</span><span class="p">.</span><span class="n">x</span> <span class="ow">and</span> <span class="n">self</span><span class="p">.</span><span class="n">y</span> <span class="o">==</span> <span class="n">other</span><span class="p">.</span><span class="n">y</span> <span class="c1"># Python set __hash__ = None automatically </span> <span class="c1"># Point is now unhashable </span> <span class="n">p</span> <span class="o">=</span> <span class="nc">Point</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="p">{</span><span class="n">p</span><span class="p">}</span> <span class="c1"># TypeError: unhashable type: 'Point' </span></code></pre> </div> <p>If you want a hashable value type, define both:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code> <span class="k">def</span> <span class="nf">__hash__</span><span class="p">(</span><span class="n">self</span><span class="p">):</span> <span class="k">return</span> <span class="nf">hash</span><span class="p">((</span><span class="n">self</span><span class="p">.</span><span class="n">x</span><span class="p">,</span> <span class="n">self</span><span class="p">.</span><span class="n">y</span><span class="p">))</span> </code></pre> </div> <p>The tuple hash is stable, consistent with equality, and composes well. Never define <code>__hash__</code> without thinking about what makes two instances equal, and never define <code>__eq__</code> without thinking about whether you also need <code>__hash__</code>.</p> <h2> Comparison Operators and the Reflected Protocol </h2> <p><code>__lt__</code>, <code>__le__</code>, <code>__gt__</code>, <code>__ge__</code>, <code>__eq__</code>, <code>__ne__</code> cover the six comparison operators. Python's mechanism for resolving them is more nuanced than most people realize.</p> <p>When you write <code>a &lt; b</code>, Python first tries <code>type(a).__lt__(a, b)</code>. If that returns <code>NotImplemented</code>, Python tries the reflected operation: <code>type(b).__gt__(b, a)</code>. This gives the right operand a chance to handle the comparison when the left operand does not know how.<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="k">class</span> <span class="nc">Celsius</span><span class="p">:</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="n">self</span><span class="p">,</span> <span class="n">temp</span><span class="p">):</span> <span class="n">self</span><span class="p">.</span><span class="n">temp</span> <span class="o">=</span> <span class="n">temp</span> <span class="k">def</span> <span class="nf">__lt__</span><span class="p">(</span><span class="n">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span> <span class="k">if</span> <span class="nf">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">Fahrenheit</span><span class="p">):</span> <span class="k">return</span> <span class="n">self</span><span class="p">.</span><span class="n">temp</span> <span class="o">&lt;</span> <span class="p">(</span><span class="n">other</span><span class="p">.</span><span class="n">temp</span> <span class="o">-</span> <span class="mi">32</span><span class="p">)</span> <span class="o">*</span> <span class="mi">5</span> <span class="o">/</span> <span class="mi">9</span> <span class="k">if</span> <span class="nf">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">Celsius</span><span class="p">):</span> <span class="k">return</span> <span class="n">self</span><span class="p">.</span><span class="n">temp</span> <span class="o">&lt;</span> <span class="n">other</span><span class="p">.</span><span class="n">temp</span> <span class="k">return</span> <span class="nb">NotImplemented</span> <span class="c1"># not NotImplemented(), the singleton </span> <span class="k">class</span> <span class="nc">Fahrenheit</span><span class="p">:</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="n">self</span><span class="p">,</span> <span class="n">temp</span><span class="p">):</span> <span class="n">self</span><span class="p">.</span><span class="n">temp</span> <span class="o">=</span> <span class="n">temp</span> <span class="k">def</span> <span class="nf">__gt__</span><span class="p">(</span><span class="n">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span> <span class="k">if</span> <span class="nf">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">Celsius</span><span class="p">):</span> <span class="k">return</span> <span class="n">self</span><span class="p">.</span><span class="n">temp</span> <span class="o">&gt;</span> <span class="n">other</span><span class="p">.</span><span class="n">temp</span> <span class="o">*</span> <span class="mi">9</span> <span class="o">/</span> <span class="mi">5</span> <span class="o">+</span> <span class="mi">32</span> <span class="k">return</span> <span class="nb">NotImplemented</span> </code></pre> </div> <p><code>NotImplemented</code> is a singleton, not an exception. Returning it tells Python to try the reflected operation. Raising <code>TypeError</code> or returning <code>False</code> are both wrong here: they prevent the fallback.</p> <p><code>functools.total_ordering</code> is worth knowing. Define <code>__eq__</code> and one of the four ordering methods, and it derives the rest. It is slower than defining all four manually but saves considerable boilerplate for types where ordering is well-defined.</p> <h2> Arithmetic Operators and In-Place Variants </h2> <p>The arithmetic protocol has three layers: forward, reflected, and in-place.</p> <p><code>a + b</code> tries <code>type(a).__add__(a, b)</code> first. If that returns <code>NotImplemented</code>, it tries <code>type(b).__radd__(b, a)</code>. The reflected methods (<code>__radd__</code>, <code>__rsub__</code>, <code>__rmul__</code>, etc.) exist so that user-defined types can work with built-in types on both sides of an operator.<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="k">class</span> <span class="nc">Vector</span><span class="p">:</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="n">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span> <span class="n">self</span><span class="p">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">x</span> <span class="n">self</span><span class="p">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">y</span> <span class="k">def</span> <span class="nf">__add__</span><span class="p">(</span><span class="n">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span> <span class="k">if</span> <span class="nf">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">Vector</span><span class="p">):</span> <span class="k">return</span> <span class="nc">Vector</span><span class="p">(</span><span class="n">self</span><span class="p">.</span><span class="n">x</span> <span class="o">+</span> <span class="n">other</span><span class="p">.</span><span class="n">x</span><span class="p">,</span> <span class="n">self</span><span class="p">.</span><span class="n">y</span> <span class="o">+</span> <span class="n">other</span><span class="p">.</span><span class="n">y</span><span class="p">)</span> <span class="k">return</span> <span class="nb">NotImplemented</span> <span class="k">def</span> <span class="nf">__radd__</span><span class="p">(</span><span class="n">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span> <span class="c1"># called when other + self and other.__add__ returned NotImplemented </span> <span class="k">return</span> <span class="n">self</span><span class="p">.</span><span class="nf">__add__</span><span class="p">(</span><span class="n">other</span><span class="p">)</span> <span class="k">def</span> <span class="nf">__mul__</span><span class="p">(</span><span class="n">self</span><span class="p">,</span> <span class="n">scalar</span><span class="p">):</span> <span class="k">if</span> <span class="nf">isinstance</span><span class="p">(</span><span class="n">scalar</span><span class="p">,</span> <span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="nb">float</span><span class="p">)):</span> <span class="k">return</span> <span class="nc">Vector</span><span class="p">(</span><span class="n">self</span><span class="p">.</span><span class="n">x</span> <span class="o">*</span> <span class="n">scalar</span><span class="p">,</span> <span class="n">self</span><span class="p">.</span><span class="n">y</span> <span class="o">*</span> <span class="n">scalar</span><span class="p">)</span> <span class="k">return</span> <span class="nb">NotImplemented</span> <span class="k">def</span> <span class="nf">__rmul__</span><span class="p">(</span><span class="n">self</span><span class="p">,</span> <span class="n">scalar</span><span class="p">):</span> <span class="k">return</span> <span class="n">self</span><span class="p">.</span><span class="nf">__mul__</span><span class="p">(</span><span class="n">scalar</span><span class="p">)</span> <span class="n">v</span> <span class="o">=</span> <span class="nc">Vector</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="nf">print</span><span class="p">(</span><span class="mi">3</span> <span class="o">*</span> <span class="n">v</span><span class="p">)</span> <span class="c1"># works because int.__mul__(3, v) returns NotImplemented, </span> <span class="c1"># then Vector.__rmul__(v, 3) is tried </span></code></pre> </div> <p>In-place operators (<code>+=</code>, <code>-=</code>, <code>*=</code>) call <code>__iadd__</code>, <code>__isub__</code>, <code>__imul__</code> and so on. If the in-place method is not defined, Python falls back to the regular method and rebinds the name. For mutable types, define the in-place variants to mutate in place and return <code>self</code>. For immutable types, do not define them and let Python handle the fallback.<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="k">def</span> <span class="nf">__iadd__</span><span class="p">(</span><span class="n">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span> <span class="k">if</span> <span class="nf">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">Vector</span><span class="p">):</span> <span class="n">self</span><span class="p">.</span><span class="n">x</span> <span class="o">+=</span> <span class="n">other</span><span class="p">.</span><span class="n">x</span> <span class="n">self</span><span class="p">.</span><span class="n">y</span> <span class="o">+=</span> <span class="n">other</span><span class="p">.</span><span class="n">y</span> <span class="k">return</span> <span class="n">self</span> <span class="c1"># must return self for in-place operations </span> <span class="k">return</span> <span class="nb">NotImplemented</span> </code></pre> </div> <h2> Attribute Access Is More Controllable Than You Think </h2> <p>The attribute access protocol has four hooks: <code>__getattribute__</code>, <code>__getattr__</code>, <code>__setattr__</code>, and <code>__delattr__</code>.</p> <p><code>__getattribute__</code> is called on every attribute access, including dunders. Overriding it without calling <code>super().__getattribute__</code> will break your class entirely. It is rarely the right hook.</p> <p><code>__getattr__</code> is called only when normal attribute lookup has failed. It is the right hook for lazy loading, proxying, and dynamic attributes:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="k">class</span> <span class="nc">LazyLoader</span><span class="p">:</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="n">self</span><span class="p">,</span> <span class="n">module_name</span><span class="p">):</span> <span class="n">self</span><span class="p">.</span><span class="n">_module_name</span> <span class="o">=</span> <span class="n">module_name</span> <span class="n">self</span><span class="p">.</span><span class="n">_module</span> <span class="o">=</span> <span class="bp">None</span> <span class="k">def</span> <span class="nf">__getattr__</span><span class="p">(</span><span class="n">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span> <span class="k">if</span> <span class="n">self</span><span class="p">.</span><span class="n">_module</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="kn">import</span> <span class="n">importlib</span> <span class="n">self</span><span class="p">.</span><span class="n">_module</span> <span class="o">=</span> <span class="n">importlib</span><span class="p">.</span><span class="nf">import_module</span><span class="p">(</span><span class="n">self</span><span class="p">.</span><span class="n">_module_name</span><span class="p">)</span> <span class="k">return</span> <span class="nf">getattr</span><span class="p">(</span><span class="n">self</span><span class="p">.</span><span class="n">_module</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span> </code></pre> </div> <p><code>__setattr__</code> is called on every attribute assignment, including in <code>__init__</code>. If you override it, you must use <code>object.__setattr__(self, name, value)</code> or <code>super().__setattr__(name, value)</code> to actually store the value, otherwise you get infinite recursion:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="k">class</span> <span class="nc">Validated</span><span class="p">:</span> <span class="k">def</span> <span class="nf">__setattr__</span><span class="p">(</span><span class="n">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span> <span class="k">if</span> <span class="n">name</span> <span class="o">==</span> <span class="sh">"</span><span class="s">age</span><span class="sh">"</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nf">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">int</span><span class="p">):</span> <span class="k">raise</span> <span class="nc">TypeError</span><span class="p">(</span><span class="sh">"</span><span class="s">age must be int</span><span class="sh">"</span><span class="p">)</span> <span class="nf">super</span><span class="p">().</span><span class="nf">__setattr__</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span> <span class="c1"># not self.name = value </span></code></pre> </div> <h2> The Container Protocol in Full </h2> <p>To make a proper sequence, you need <code>__len__</code> and <code>__getitem__</code>. Python derives a surprising amount from these two:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="k">class</span> <span class="nc">Fibonacci</span><span class="p">:</span> <span class="k">def</span> <span class="nf">__len__</span><span class="p">(</span><span class="n">self</span><span class="p">):</span> <span class="k">return</span> <span class="mi">100</span> <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="n">self</span><span class="p">,</span> <span class="n">index</span><span class="p">):</span> <span class="k">if</span> <span class="nf">isinstance</span><span class="p">(</span><span class="n">index</span><span class="p">,</span> <span class="nb">slice</span><span class="p">):</span> <span class="k">return</span> <span class="p">[</span><span class="n">self</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nf">range</span><span class="p">(</span><span class="o">*</span><span class="n">index</span><span class="p">.</span><span class="nf">indices</span><span class="p">(</span><span class="nf">len</span><span class="p">(</span><span class="n">self</span><span class="p">)))]</span> <span class="k">if</span> <span class="n">index</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span> <span class="n">index</span> <span class="o">+=</span> <span class="nf">len</span><span class="p">(</span><span class="n">self</span><span class="p">)</span> <span class="k">if</span> <span class="ow">not</span> <span class="mi">0</span> <span class="o">&lt;=</span> <span class="n">index</span> <span class="o">&lt;</span> <span class="nf">len</span><span class="p">(</span><span class="n">self</span><span class="p">):</span> <span class="k">raise</span> <span class="nc">IndexError</span><span class="p">(</span><span class="n">index</span><span class="p">)</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nf">range</span><span class="p">(</span><span class="n">index</span><span class="p">):</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="k">return</span> <span class="n">a</span> <span class="n">fib</span> <span class="o">=</span> <span class="nc">Fibonacci</span><span class="p">()</span> <span class="nf">print</span><span class="p">(</span><span class="n">fib</span><span class="p">[</span><span class="mi">10</span><span class="p">])</span> <span class="c1"># 55 </span><span class="nf">print</span><span class="p">(</span><span class="n">fib</span><span class="p">[</span><span class="mi">2</span><span class="p">:</span><span class="mi">5</span><span class="p">])</span> <span class="c1"># [1, 2, 3] </span><span class="nf">print</span><span class="p">(</span><span class="mi">10</span> <span class="ow">in</span> <span class="n">fib</span><span class="p">)</span> <span class="c1"># works: Python iterates using __getitem__ </span><span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">fib</span><span class="p">:</span> <span class="c1"># works: iteration falls back to __getitem__ </span> <span class="k">pass</span> </code></pre> </div> <p><code>in</code> and <code>for</code> both fall back to <code>__getitem__</code>-based iteration if <code>__contains__</code> and <code>__iter__</code> are not defined. Python calls <code>__getitem__</code> with increasing integer indices starting at 0 until it gets an <code>IndexError</code>. This fallback exists for backward compatibility but you should define <code>__iter__</code> explicitly for anything you intend to be iterable.</p> <p>For mappings, define <code>__getitem__</code>, <code>__setitem__</code>, <code>__delitem__</code>, <code>__len__</code>, and <code>__iter__</code>. Inheriting from <code>collections.abc.MutableMapping</code> and implementing those five methods gives you <code>keys()</code>, <code>values()</code>, <code>items()</code>, <code>get()</code>, <code>update()</code>, <code>pop()</code>, and the rest for free through mixin implementations.</p> <h2> <strong>slots</strong>: Memory Layout as a Data Model Feature </h2> <p><code>__slots__</code> is part of the data model. Defining it on a class prevents the creation of <code>__dict__</code> on instances and instead allocates a fixed set of descriptors for the named attributes.<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight python"><code><span class="k">class</span> <span class="nc">Point</span><span class="p">:</span> <span class="n">__slots__</span> <span class="o">=</span> <span class="p">(</span><span class="sh">"</span><span class="s">x</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">y</span><span class="sh">"</span><span class="p">)</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="n">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span> <span class="n">self</span><span class="p">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">x</span> <span class="n">self</span><span class="p">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">y</span> <span class="n">p</span> <span class="o">=</span> <span class="nc">Point</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="n">p</span><span class="p">.</span><span class="n">z</span> <span class="o">=</span> <span class="mi">3</span> <span class="c1"># AttributeError: 'Point' object has no attribute 'z' </span></code></pre> </div> <p>The memory savings are significant for classes with many instances. A regular instance carries a <code>__dict__</code> (a hash table) and a <code>__weakref__</code> slot. A slotted instance carries only the declared attributes. For a class like <code>Point</code> that might be instantiated millions of times, this matters.</p> <p><code>__slots__</code> interacts with inheritance in ways that require care. If a subclass does not define <code>__slots__</code>, instances get a <code>__dict__</code> again, defeating the purpose. Every class in the hierarchy needs to define <code>__slots__</code> for the optimization to hold all the way down.</p> <h2> The One Rule That Ties Everything Together </h2> <p>Dunder methods are a protocol, not a feature list. Every method participates in a documented contract. <code>__eq__</code> implies <code>__hash__</code> constraints. <code>__iter__</code> implies <code>__next__</code>. <code>__enter__</code> implies <code>__exit__</code>. <code>__get__</code> participating in the descriptor protocol implies understanding when <code>obj</code> is <code>None</code>.</p> <p>The mistakes come from implementing part of a protocol and ignoring the rest. Define <code>__eq__</code> without thinking about <code>__hash__</code> and your objects silently break in sets. Define <code>__iter__</code> without <code>StopIteration</code> handling and your loops never end. Define <code>__enter__</code> without a matching <code>__exit__</code> and resources leak.</p> <p>Read the protocol documentation for any dunder you implement. The contracts are specified, they are not long, and violating them produces bugs that are difficult to reproduce and harder to trace. The data model is the most reliable part of Python. It rewards the developers who actually read it.</p> <h2> Further Reading </h2> <ul> <li> <a href="https://docs.python.org/3/reference/datamodel.html" rel="noopener noreferrer">Python Data Model (docs.python.org)</a> - read this end to end at least once, it is shorter than you expect</li> <li> <a href="https://www.oreilly.com/library/view/fluent-python-2nd/9781492056348/" rel="noopener noreferrer">Luciano Ramalho: Fluent Python, Part I</a> - the best book-length treatment of the data model with worked examples throughout</li> <li> <a href="https://docs.python.org/3/library/collections.abc.html" rel="noopener noreferrer">collections.abc documentation</a> - the abstract base classes that define the container protocols formally</li> <li> <a href="https://www.youtube.com/watch?v=HTLu2DFOdTg" rel="noopener noreferrer">Raymond Hettinger: Python's Class Development Toolkit (PyCon 2013)</a> - still the best talk on building well-behaved Python classes</li> </ul>