1 / 11
Go beyond Python basics and write code like an experienced Pythonista: idiomatic collections, comprehensions, unpacking, the data model and dunder methods, operator overloading, properties and descriptors, iterators, generators, itertools, context managers, first-class functions, closures…
Intermediate Idioms, the Data Model, Iterators, Decorators, and Writing Code Like an Experienced Pythonista
Read before you buy: the PDF here is a free sample — the first 20 pages of the 571-page book (payments, sales and reviews on this demo site are sample data). Get the full e-book at thenexbooks on Gumroad.
You already know Python. This book teaches you to write it fluently.
Your code works and your tests pass — yet an experienced Python developer could rewrite twenty of your lines as four, with a comprehension, a with block, or a class that suddenly works with len(), for, and sorted(). Nothing they use is secret. They know how Python is designed to be used: its data model, its protocols, and its standard library.
This book teaches that knowledge. It skips the beginner material and goes straight to the ideas that separate everyday Python from expert Python, always showing the un-Pythonic habit next to the idiomatic version and explaining why the idiom is better: clearer, safer, and familiar to every Python developer who reads it.
What's inside
- What "Pythonic" really means, PEP 8 and the Zen of Python in practice, and linting with ruff
- Names, objects, and references: identity vs equality, mutability, aliasing bugs, the mutable default argument trap, shallow and deep copies
- Collections used well: choosing lists, tuples, dicts, and sets; dict and set idioms; sorting with key functions
- Comprehensions, generator expressions, unpacking, slicing,
enumerate,zip(strict=True), and the walrus operator - The Python data model:
__repr__,__eq__,__hash__, ordering, container protocols, truthiness, and operator overloading with a realMoneytype - Attribute access: properties, descriptors,
__getattr__, and__slots__ - Iterators, generators,
yield from, lazy pipelines, the itertools toolbox (includingbatchedandpairwise), and context managers with contextlib - Functions as objects: parameters and signatures, keyword-only and positional-only arguments, closures, decorators from first principles to typed decorator factories, and functools (
partial,cache,singledispatch,cached_property) - Pythonic types: dataclasses done right, enums and flags, structural pattern matching,
Counter,defaultdict,deque,namedtuple,ChainMap, abstract base classes, andtyping.Protocol - Exceptions done right: hierarchies, EAFP vs LBYL, chaining,
add_note(), exception groups andexcept* - Modules, packages, and imports; f-strings and the format spec mini-language; Unicode and encodings
- A complete refactoring of a real un-Pythonic script, step by step, protected by a golden-output test
One running example, start to finish. You maintain the box-office code of Mapleridge Cinema, a fictional independent cinema with three screens, six films, a weekend of screenings, forty tickets, matinee discounts, premium recliner seats, and a busy concession stand. Each chapter improves the code base — a seat map that works with len() and in, a Money type with real arithmetic, generators that read data lazily, decorators that log and retry, dataclasses and enums for the domain, pattern matching for box-office commands — until the final chapter turns the cinema's original report script into clean, idiomatic Python. Every example was run, and every output in the book is real.
Current and honest. The book targets Python 3.12 and newer, and says which version introduced each modern feature; features of the newest releases are clearly labeled. It contains no invented benchmarks or statistics. Every chapter includes runnable examples with their output, Tips, Notes, Warnings, "Try It Yourself" exercises, and Key Takeaways, and the appendices give you an idioms cheat sheet, reference tables and templates, a Pythonic code review checklist, and a glossary.
Format: PDF, 571 pages (A4), linked table of contents and bookmarks. English. First Edition, 2026. Published by thenexbooks. This book is independent and is not affiliated with, sponsored by, or endorsed by the Python Software Foundation.
Table of Contents
Part I — Thinking in Python
- Ch 1 · What "Pythonic" Means: Idioms, the Zen, and the Mapleridge Code Base
- Ch 2 · Names, Objects, and References: Identity, Mutability, and Copying
- Ch 3 · Collections Used Well: Lists, Tuples, Dicts, and Sets
- Ch 4 · Comprehensions and Generator Expressions
- Ch 5 · Unpacking, Slicing, and Sequence Idioms
Part II — The Data Model
- Ch 6 · The Python Data Model: How Dunder Methods Make Objects Feel Built-In
- Ch 7 · Representation, Equality, Hashing, and Ordering
- Ch 8 · Container Protocols: Sized, Iterable, Subscriptable, and Truthy Objects
- Ch 9 · Operator Overloading: Building the Money Type
- Ch 10 · Attribute Access: Properties, Descriptors, and slots
Part III — Iteration and Resources
- Ch 11 · Iterators and the Iteration Protocol
- Ch 12 · Generators: yield, Lazy Pipelines, and yield from
- Ch 13 · itertools and Lazy Data Processing
- Ch 14 · Context Managers: with, contextlib, and Resource Safety
Part IV — Functions as Objects
- Ch 15 · Functions as First-Class Objects: Callables, Parameters, and Signatures
- Ch 16 · Closures, Scope, and nonlocal
- Ch 17 · Decorators from First Principles
- Ch 18 · Advanced Decorators: Arguments, Classes, Stacking, and Typing
- Ch 19 · functools in Practice: partial, Caching, singledispatch, and reduce
Part V — Designing Pythonic Types
- Ch 20 · Dataclasses Done Right
- Ch 21 · Enums and Named Constants
- Ch 22 · Structural Pattern Matching with match and case
- Ch 23 · The collections Module: Counter, defaultdict, deque, namedtuple, and ChainMap
- Ch 24 · Abstract Base Classes and Protocols: Duck Typing with Guarantees
Part VI — Pythonic Code in Practice
- Ch 25 · Exceptions Done Right: Hierarchies, EAFP, Chaining, and Exception Groups
- Ch 26 · Modules, Packages, and Imports
- Ch 27 · Text and String Formatting: f-Strings, Format Specs, and Unicode
- Ch 28 · Refactoring Un-Pythonic Code: The Mapleridge Report Case Study
Appendices
A · Pythonic Idioms Cheat Sheet
B · Templates and Reference
C · Pythonic Code Review Checklist
D · Glossary
Book details
| Detail | |
|---|---|
| Pages | 571 |
| Format | PDF (A4) |
| Language | English |
| Level | Intermediate |
| Edition | 1st Edition (2026) |
| Download on this site | Free sample, first 20 pages (PDF) |
| Publisher | thenexbooks |
Files you get
No reviews yet. Only buyers can review.
More from thenexbooks