Python's data model for MATLAB and DSP engineers: a free guide to dunder methods, protocols, descriptors and CPython internals.
Read it online · Download the PDF
Much of Python's elegance comes from one design decision: the syntax is fixed, but what the syntax does is decided by your objects. This guide starts from that idea and follows it down to the C structs in CPython and up to a working signal-processing block.
- How does
selfget filled in, when Python has no special machinery for methods? - What is a descriptor, and how do
property,classmethodandstaticmethoduse them? - Why does
3 * Gain(2)work, whenintwas written decades before your class? - Why does giving one object its own
__len__do nothing? - Why can two small methods make
for,in,reversed,sortedandmaxall work on your class? - Why does
x * my_objectsometimes return a slow NumPy array of dtypeobject, and how do you stop it? - How do you build a MATLAB System object in Python, with nontunable properties and
setupImpl/stepImpl? - Is Python fast enough compared with MATLAB, and when do you need Numba, CuPy, Cython or pybind11?
- What does this design cost you, from the GIL to memory use and late errors?
Engineers who can already write Python, or who are moving to it from MATLAB, C, C++ or Java, and want to understand why it behaves as it does. The examples come from DSP and GNSS work, such as filters, delay lines and streaming large recordings, but the Python applies to any field.
- The Core Philosophy: Fixed Syntax, Pluggable Behavior: dunder methods and protocols
- Under the Hood: The C-Struct Architecture:
PyObject, reference counting and garbage collection - The Grand Unification: Two Pillars and a Keystone: descriptors, bound methods and namespaces
- Deep Dive: First-Class Objects: closures, and Python compared with C, C++ and Java
- Conceptual Paradigm: Python as a Plugin Architecture: duck typing and abstract base classes
- The Data Model, Protocols and Dunder Methods: operator dispatch, context managers and
__array_ufunc__ - Python as an ICD: The Client/Supplier Dynamic: the data model as an interface contract
- The Golden Rule of Dunders: when to call them directly, and when not to
- Practical Synthesis: The MATLAB System Object in Python: a full worked example with a delay line
- IDE Integration and Essential Protocols: The Cheat Sheet: type hints, generators and
__slots__ - Performance Reality: Python vs. MATLAB: measured NumPy overhead, Numba, CuPy and GPU code
- What the Design Costs: the GIL, memory, late errors and the limits of the model
- Epilogue: Why "Data Model"?
The guide is one Markdown file, The Python Architecture Why It Works So Elegantly.md. build.py turns it into the website and PDF in _site/, and every push to main deploys them to GitHub Pages.
PDF generation needs WeasyPrint's system dependencies. With those installed, using Python 3.12:
python -m venv .venv
source .venv/bin/activate # Windows (Git Bash): source .venv/Scripts/activate
pip install -r requirements.txt
python build.py