Writing

Books on Python and Metaprogramming

I write long-form technical material drawn from years of teaching, software engineering and thinking about how systems work. These books are practical, concept-driven and free to read online.

Free online book

Metaprogramming: An Adventure

  • ~250 pages5–10 hours
  • 500+code examples
  • 100+demos

Metaprogramming is the practice of writing code that operates on other code: inspecting, modifying, even generating programs from scratch. It's incredibly powerful, somewhat underrated and understudied – and most importantly, incredibly fun.

I've been fascinated with this sorcery since I first encountered it. I've written plenty of useful code employing it, but the moment it became "work", it shifted into the realm of "advanced programming techniques". This book contains a bunch of those practices, but I've tried to tell it as an adventure.

For me, metaprogramming isn't something to learn for any serious purpose. Concrete goals narrow our vision; playing lets our imagination run wild, experiment and stumble onto innovation. We can later refine those discoveries into practical techniques, but novelty often begins with uninhibited, irreverent joy.

That joy is what I'd like to impart: a sense of wonder. Even with AI doing most of our coding nowadays, I hope you'll enjoy these strange and beautiful ideas as much as I enjoyed exploring them.

Inside the book

  • Tracerspreserve attributes definition order; implement C-style enums in Python; link nested lexical scopes.
  • Bytecode manipulationcode objects; rewriting bytecode and its optimizations; classes with implicit self; rewiring scopes; injecting code into a running program.
  • Dynamic executioneval and exec; walking the call stack; security and sandboxing; web notebooks; ASTs and transformers.
  • Shell autocompletionin Bash and in Python; a small completion language; supporting paths; dynamic extensions.
  • Aspect-oriented programmingweaving behavior into code with decorators and code analysis; dependency injection, setup and teardown; parsing tracebacks; framework design.
  • Domain-specific languagesplugins; regular expressions; context-free grammars; adding syntax the Python interpreter; transpiling Python code.
  • A metaprogramming layerautomatic logging with tracers, decorators, dynamic execution, and ASTs; user-defined code generation.
  • Source code manipulationautomatic decoration; turning async code into sync code; normalizing imports, circular imports, and lazy imports.
  • Runtime serializationpickling and its limits; patching memory; pickling generators; fast-forwarding generators; snapshots and durable execution.
  • Templating and code generationa templating engine in 20 lines; a metaprogramming engine with templates, macros, plugins and custom interpolation; generating directory structures, a web framework, and standalone code.
Read for free

Free online book

The Unbearable Lightness of Coding

  • 1,000+ pages16–40 hours
  • 2,000+code examples
  • 150+exercises

Python was one of the first languages I learned – and then kept learning for years. Its elegance and versatility kept surprising me; from design patterns to language internals, it seemed the perfect medium for exploring software engineering at large.

This book grew out of ten years of teaching in industry and academia: a comprehensive, practical and occasionally philosophical overview of this wonderful language. Somewhat tragically, I finished it just as LLMs arrived, and with agents doing most of our coding, it lost some of its luster.

Still, I think there's value in learning to code – it certainly benefits me in my work with AI. It's less about syntax or libraries than a way of reasoning about problems, which still seems to me like the king's road to building good software.

As we reimagine our profession, I hope this guide helps those curious about how their ancestors used to type programs, slowly and manually – and the thinking that informed, and still informs, the paradigm shift we're living through.

Inside the book

  • The corenames, values and scopes; conditions and boolean fluency; loops.
  • Functionssignatures as interfaces; functional programming; code as an object; decorators; generators, from iterators to coroutines.
  • The object modelIDs, types and values; inheritance; magic methods; attributes, methods and descriptors; object lifecycle and context managers; metaclasses.
  • The language at largemodules, from dynamic evaluation to packages; exceptions, tracebacks and frames; native types; the standard library (datetimes, paths, regular expressions); the walrus operator; match-case; type hinting.
  • Practicehow to structure and run a real Python project.
  • Performanceprofiling; optimizing orders and constants; JIT, FFIs and PyPy.
  • Networkinghow the internet works; sockets; building clients and servers from scratch.
  • Concurrencythreads and locks; the GIL and free-threaded Python; Amdahl's law and the limits of concurrency; multiprocessing.
  • Asynchronous ProgrammingI/O vs. CPU load; select, poll and epoll; state machines and their limits; callbacks; coroutines from generators; asyncio, task and futures.
  • The webhow the web works; HTTP, HTML, CSS and JS; RESTful APIs; writing a web server from scratch up to Flask.
  • InterfacesCLIs, from scratch to Click; GUIs (and why the answer is usually Javascript).
  • Software designdesign patterns, from the Gang of Four to whether we still need them; complexity, where it comes from and who pays for it.
Read for free

Let’s work together

Consulting and corporate training

Get in touch if you're looking for custom training for your team
or a software architecture review for your business.