Kick off your book project in 3 hours! Live workshop on Zoom. You’ll leave with a real book project, progress on your first chapter, and a clear plan to keep going. Saturday, June 6, 2026. Learn more…

Leanpub Header

Skip to main content

Python Engineering Judgment

A Field Guide to Runtime Behavior, Design Tradeoffs, and Production Failure Modes

This book is 70% completeLast updated on 2026-05-03

Python Engineering Judgment is for engineers who already know Python and want to build the skill AI makes more valuable: the ability to reason about code, systems, tradeoffs, and failure modes.

This deep, practical field guide teaches the Python object model, type system, decorators, generators, concurrency, asyncio, multiprocessing, API design, packaging, performance, debugging, security, observability, and reliability through the lens of real-world engineering judgment.

Use it to prepare for senior, staff, principal, and tech-lead interviews, and to make better decisions in code reviews and production systems.

Minimum price

$29.99

$49.99

You pay

Author earns

$
PDF
EPUB
WEB
About

About

About the Book

Python Engineering Judgment is a field guide for engineers who already know Python and want to develop the skill AI makes more valuable: judgment.

AI can generate plausible Python quickly. But plausible code is not the same as correct, maintainable, observable, secure code, or code that fits the system around it. As code generation becomes cheaper, the scarce skill becomes deciding what should exist, how it should behave, what tradeoffs it accepts, and how it will fail.

This book is not a beginner’s Python tutorial or a syntax reference. It teaches Python through the decisions that matter in real systems: runtime behavior, abstraction design, concurrency hazards, performance bottlenecks, packaging problems, debugging pressure, security risks, observability gaps, and production failure modes.

Use it to prepare for senior, staff, principal, and tech-lead interviews. Use it to become sharper in code reviews. Use it to make better production decisions. Use it to move beyond knowing Python and toward thinking with Python.

Author

About the Author

Contents

Table of Contents

Chapter 1 — The Python Object Model

  1. 1.1 Everything Is an Object
  2. 1.2 Attribute Lookup: The Full Resolution Order
  3. 1.3 Special Methods (Dunder Methods)
  4. 1.4 __slots__
  5. 1.5 Function Objects in Depth
  6. 1.6 Attribute Access Hooks
  7. 1.7 @property, @staticmethod, @classmethod
  8. 1.8 __init_subclass__ and Class Hooks
  9. 1.9 The object.__class__ and type.__instancecheck__
  10. 1.10 Object Copying
  11. 1.11 Identity, Equality, and Interning
  12. Summary of Key Principles

Chapter 2 — The Type System & Type Hints

  1. 2.1 The Purpose of Type Hints in Python
  2. 2.2 Annotation Mechanics
  3. 2.3 Core Typing Constructs
  4. 2.4 Generics
  5. 2.5 Structural Typing with Protocol
  6. 2.6 TypedDict
  7. 2.7 dataclass vs NamedTuple vs TypedDict
  8. 2.8 @overload
  9. 2.9 Abstract Base Classes and typing.abc
  10. 2.10 Annotating Callables
  11. 2.11 Runtime Type Checking
  12. 2.12 mypy Configuration and Usage Patterns
  13. 2.13 Common Typing Mistakes and Misconceptions
  14. 2.14 Typing Patterns for Interview Code
  15. Summary

Chapter 3 — Functional Programming: Comprehensions, functools, itertools

  1. 3.1 What “Functional” Means in Python
  2. 3.2 Comprehensions
  3. 3.3 functools
  4. 3.4 itertools
  5. 3.5 Building an Iterator Algebra
  6. 3.6 operator Module
  7. 3.7 Higher-Order Function Patterns
  8. 3.8 map and filter
  9. 3.9 Performance Reference
  10. 3.10 Common Interview Applications
  11. Summary

Chapter 4 — Iterators, Generators & Coroutines

  1. 4.1 The Iterator Protocol
  2. 4.2 Generator Functions
  3. 4.3 yield from
  4. 4.4 Practical Generator Patterns
  5. 4.5 Async Generators
  6. 4.6 contextlib Utilities
  7. 4.7 Advanced Generator Internals
  8. 4.8 Typing Generators and Iterators
  9. 4.9 Common Mistakes and Pitfalls
  10. 4.10 Complete Example: Lazy Streaming Pipeline
  11. Summary

Chapter 5 — Decorators & Metaclasses

  1. 5.1 What a Decorator Is
  2. 5.2 Function Decorators
  3. 5.3 Class Decorators
  4. 5.4 Stacking Decorators
  5. 5.5 Descriptors as Decorators
  6. 5.6 The type Metaclass
  7. 5.7 Writing Metaclasses
  8. 5.8 Abstract Base Classes
  9. 5.9 __init_subclass__ — Metaclass-Lite
  10. 5.10 Decorator Patterns Reference
  11. 5.11 Decorator Anti-Patterns
  12. 5.12 Choosing the Right Tool
  13. 5.13 Complete Example: A Production-Grade Decorator Stack
  14. Summary

Chapter 6 — Memory Management & CPython Internals

  1. 6.1 Why CPython Internals Matter at Staff Level
  2. 6.2 Reference Counting
  3. 6.3 The Cyclic Garbage Collector
  4. 6.4 CPython Memory Allocator
  5. 6.5 String Interning
  6. 6.6 Integer Interning
  7. 6.7 The dis Module — Bytecode Inspection
  8. 6.8 The Import System
  9. 6.9 The Global Interpreter Lock (Preview)
  10. 6.10 Profiling and Memory Tracing
  11. 6.11 Object Lifecycle Patterns and Leak Detection
  12. 6.12 __del__, weakref.finalize, and Safe Cleanup
  13. 6.13 Slots Revisited — Memory Layout
  14. Summary

Chapter 7 — The GIL: A Precise Model

  1. 7.1 What the GIL Is
  2. 7.2 Why the GIL Exists
  3. 7.3 The Execution Model: When the GIL Is Held and Released
  4. 7.4 Threading with the GIL: What Actually Happens
  5. 7.5 The GIL and C Extensions
  6. 7.6 Alternatives to the GIL for CPU-Bound Parallelism
  7. 7.7 Atomicity Under the GIL
  8. 7.8 The GIL and asyncio
  9. 7.9 Diagnosing GIL-Related Performance Problems
  10. 7.10 The GIL in Practice: Decision Framework
  11. 7.11 Common Misconceptions
  12. Summary

Chapter 8 — Threading: Correct Concurrent Design

  1. 8.1 The threading Module
  2. 8.2 Thread — Lifecycle and Configuration
  3. 8.3 Lock — Mutual Exclusion
  4. 8.4 RLock — Reentrant Lock
  5. 8.5 Semaphore and BoundedSemaphore
  6. 8.6 Event — Thread Signalling
  7. 8.7 Condition — Monitor Pattern
  8. 8.8 Barrier — Phase Synchronisation
  9. 8.9 threading.local — Thread-Local Storage
  10. 8.10 Deadlock
  11. 8.11 Livelock and Starvation
  12. 8.12 Correct Thread-Safe Patterns
  13. 8.13 threading.Timer
  14. 8.14 threading.excepthook — Unhandled Thread Exceptions
  15. 8.15 Complete Example: Thread-Safe Web Crawler
  16. Summary

Chapter 9 — concurrent.futures & Queue-Based Concurrency

  1. 9.1 The Abstraction Hierarchy
  2. 9.2 concurrent.futures — Core Concepts
  3. 9.3 ThreadPoolExecutor in Depth
  4. 9.4 ProcessPoolExecutor in Depth
  5. 9.5 queue Module — Production Queue Types
  6. 9.6 Producer-Consumer Patterns
  7. 9.7 Backpressure
  8. 9.8 concurrent.futures vs queue — Decision Guide
  9. 9.9 Error Handling in Futures
  10. 9.10 Monitoring and Observability
  11. 9.11 Complete Example: Parallel Crawler with ProcessPoolExecutor
  12. Summary

Chapter 10 — Asyncio: Deep Dive

  1. 10.1 The Concurrency Model
  2. 10.2 Coroutines, Tasks, and the Event Loop
  3. 10.3 gather, wait, and as_completed
  4. 10.4 Cancellation
  5. 10.5 Synchronisation Primitives
  6. 10.6 run_in_executor — Bridging Sync and Async
  7. 10.7 Event Loop Internals
  8. 10.8 Structured Concurrency (Python 3.11+ TaskGroup)
  9. 10.9 asyncio Patterns
  10. 10.10 Mixing asyncio with Threads
  11. 10.11 Error Handling and Debugging
  12. 10.12 asyncio and aiohttp — HTTP Client Pattern
  13. Summary

Chapter 11 — Multiprocessing

  1. 11.1 When Multiprocessing Is the Right Tool
  2. 11.2 Start Methods
  3. 11.3 Process — Individual Processes
  4. 11.4 Pool — Worker Process Pools
  5. 11.5 Inter-Process Communication
  6. 11.6 multiprocessing.shared_memory — Zero-Copy Shared Data
  7. 11.7 mp.Manager — Managed Shared Objects
  8. 11.8 Synchronisation Across Processes
  9. 11.9 ProcessPoolExecutor vs mp.Pool
  10. 11.10 Pickling Deep Dive
  11. 11.11 Common Failure Modes
  12. 11.12 Complete Example: Parallel Document Processor
  13. Summary

Chapter 12 — Designing Pythonic APIs

  1. 12.1 What Makes an API Pythonic
  2. 12.2 Function Signatures
  3. 12.3 Return Types
  4. 12.4 Lazy vs Eager APIs
  5. 12.5 Context Managers as API
  6. 12.6 Error Handling in APIs
  7. 12.7 Naming and Conventions
  8. 12.8 Versioning and Backward Compatibility
  9. 12.9 Configuration and Dependency Injection
  10. 12.10 Operator Overloading
  11. 12.11 Introspection and __repr__
  12. 12.12 Complete Example: A Pythonic HTTP Client API
  13. Summary

Chapter 13 — Object-Oriented Design & Patterns

  1. 13.1 Design Patterns in a Python Context
  2. 13.2 SOLID Principles in Python
  3. 13.3 Creational Patterns
  4. 13.4 Structural Patterns
  5. 13.5 Behavioural Patterns
  6. 13.6 Composition vs Inheritance
  7. 13.7 Complete Example: A Plugin-Based Processing System
  8. Summary

Chapter 14 — Designing for Change & Extension

  1. 14.1 The Core Problem
  2. 14.2 Identifying and Isolating Change Axes
  3. 14.3 Extension Points
  4. 14.4 Configuration Management
  5. 14.5 Feature Flags
  6. 14.6 Dependency Management and Import Discipline
  7. 14.7 Versioning API Contracts
  8. 14.8 Module and Package Structure
  9. 14.9 Testing as a Design Signal
  10. 14.10 Backward Compatibility Checklist
  11. 14.11 Complete Example: Extensible Middleware System
  12. Summary

Chapter 15 — Packaging & Distribution

  1. 15.1 Why Packaging Matters at Staff Level
  2. 15.2 The Modern Python Package Ecosystem
  3. 15.3 pyproject.toml — The Complete Reference
  4. 15.4 Version Management
  5. 15.5 Dependency Specification
  6. 15.6 Project Layout
  7. 15.7 Entry Points
  8. 15.8 Distribution Formats
  9. 15.9 Type Information Distribution (PEP 561)
  10. 15.10 Virtual Environments and Isolation
  11. 15.11 CI/CD Packaging Pipeline
  12. 15.12 CLI Design
  13. 15.13 Reproducible Builds and Supply Chain Security
  14. 15.14 Internal Packages and Private Registries
  15. 15.15 Complete pyproject.toml Reference
  16. Summary

Chapter 16 — Performance Engineering

  1. 16.1 The Performance Engineering Mindset
  2. 16.2 Understanding Python’s Performance Characteristics
  3. 16.3 Profiling Tools
  4. 16.4 Common Bottlenecks and Their Fixes
  5. 16.5 Data Structure Performance
  6. 16.6 Caching Strategies
  7. 16.7 Algorithmic Complexity
  8. 16.8 Python-Specific Optimisation Techniques
  9. 16.9 Concurrency as a Performance Tool
  10. 16.10 Cython, Numba, and C Extensions
  11. 16.11 Benchmarking Best Practices
  12. 16.12 Complete Example: Profiling and Optimising a URL Processor
  13. Summary

Chapter 17 — Debugging Under Pressure

  1. 17.1 The Debugging Mindset
  2. 17.2 Reading Tracebacks
  3. 17.3 pdb — The Python Debugger
  4. 17.4 logging — Structured Observability
  5. 17.5 sys._current_frames() and Stack Inspection
  6. 17.6 faulthandler — Segfault and Signal Debugging
  7. 17.7 Debugging Concurrency Bugs
  8. 17.8 inspect Module — Runtime Introspection
  9. 17.9 dis Module — Bytecode Inspection for Debugging
  10. 17.10 Remote Debugging and Production Diagnostics
  11. 17.11 Binary Search Debugging
  12. 17.12 Debugging Checklist
  13. 17.13 Complete Example: Debugging a Production Race Condition
  14. Summary

Chapter 18 — Failure Mode Thinking & Resilience Patterns

  1. 18.1 The Failure Mode Mindset
  2. 18.2 Timeout Everywhere
  3. 18.3 Retry with Exponential Backoff and Jitter
  4. 18.4 Circuit Breaker
  5. 18.5 Rate Limiting
  6. 18.6 Bulkhead Pattern
  7. 18.7 Graceful Degradation
  8. 18.8 Load Shedding
  9. 18.9 Backpressure
  10. 18.10 Graceful Shutdown
  11. 18.11 Health Checks
  12. 18.12 Failure Mode Analysis: The Web Crawler
  13. 18.13 Complete Example: Resilient HTTP Client
  14. Summary
  15. Part V — Production-Grade Engineering

Chapter 19 — Security Awareness

  1. 19.1 Security as a Design Property
  2. 19.2 Injection Vulnerabilities
  3. 19.3 pickle — Arbitrary Code Execution
  4. 19.4 Path Traversal
  5. 19.5 Server-Side Request Forgery (SSRF)
  6. 19.6 Secrets Management
  7. 19.7 Input Validation
  8. 19.8 Cryptography
  9. 19.9 Dependency Security
  10. 19.10 Web Security
  11. 19.11 Logging Security
  12. 19.12 Security Checklist
  13. 19.13 Complete Example: Secure Web Crawler Configuration
  14. Summary

Chapter 20 — Observability & Monitoring

  1. 20.1 The Three Pillars of Observability
  2. 20.2 Structured Logging
  3. 20.3 Metrics
  4. 20.4 Distributed Tracing
  5. 20.5 Health Checks and Readiness Probes
  6. 20.6 Alerting
  7. 20.7 SLIs, SLOs, and Error Budgets
  8. 20.8 structlog — Production Structured Logging
  9. 20.9 Runbooks and On-Call Readiness
  10. 20.10 Complete Example: Fully Observable Crawler
  11. Summary

Chapter 21 — Reliability Concepts & Distributed Patterns

  1. 21.1 From Single-Process to Distributed Systems
  2. 21.2 The CAP Theorem and Its Practical Meaning
  3. 21.3 Consistency Models
  4. 21.4 Distributed Transactions
  5. 21.5 Consistent Hashing
  6. 21.6 Message Queues and Event-Driven Architecture
  7. 21.7 Leader Election
  8. 21.8 Service Discovery and Configuration
  9. 21.9 Horizontal Scaling Patterns
  10. 21.10 Data Replication and Read Scaling
  11. 21.11 Distributed Rate Limiting
  12. 21.12 The Twelve-Factor App
  13. 21.13 Complete Example: Distributed Crawler Architecture
  14. Summary

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub