Start free trial
Trending New Genres Authors
EnglishEnglish
EspañolSpanish
简体中文Chinese
繁體中文Chinese (Traditional)
FrançaisFrench
DeutschGerman
日本語Japanese
PortuguêsPortuguese
ItalianoItalian
한국어Korean
РусскийRussian
NederlandsDutch
العربيةArabic
PolskiPolish
हिन्दीHindi
Tiếng ViệtVietnamese
SvenskaSwedish
ΕλληνικάGreek
TürkçeTurkish
ไทยThai
ČeštinaCzech
RomânăRomanian
MagyarHungarian
УкраїнськаUkrainian
IndonesiaIndonesian
DanskDanish
SuomiFinnish
БългарскиBulgarian
עבריתHebrew
NorskNorwegian
HrvatskiCroatian
CatalàCatalan
SlovenčinaSlovak
LietuviųLithuanian
SlovenščinaSlovenian
СрпскиSerbian
EestiEstonian
LatviešuLatvian
فارسیPersian
മലയാളംMalayalam
தமிழ்Tamil
اردوUrdu
Searching...
SoBrief
Python Cookbook

Python Cookbook

338 community-vetted Python fixes, each with the tradeoffs most copy-and-paste coders never see.
by David Beazley 2002 807 pages
4.18
1k+ ratings
Listen
Immersive
V2.3
Amazon Kindle Audible
Try Full Access for 3 Days
Unlock listening & more!
Continue

Key Takeaways

Use small, tested recipes as building blocks, not finished architecture

A field manual, not a manifesto. This book gathers practical answers to recurring Python problems, from string handling and file I/O to databases, concurrency, XML, and GUIs. Its second edition contains 338 recipes: 146 revised from the first edition and 192 new, selected from nearly 1,000 online contributions. More than 300 people contributed to the broader project, with community feedback helping recipes mature before publication.

Read by need or by theme. The 20 chapters group recipes by task, but the editors also designed the book for readers who dip in at random. Each entry offers a problem, a solution, and discussion of choices, caveats, or alternatives. The practical lesson is to borrow a working pattern, understand its assumptions, and adapt it to the job at hand.

Analysis

What's distinctive is the book's editorial method: it treats programming knowledge as something tested and improved in public, rather than as a single author's uninterrupted doctrine. That makes the collection unusually rich in competing approaches and small, reusable techniques. It also means the recipes vary in voice, depth, and assumptions. A recipe is a starting point, not a substitute for understanding the surrounding system, especially when code comes from an edition centered on Python 2.3 and 2.4.

Program to behavior, not exact types, and let exceptions expose mismatches

Trust the interface. Python code often works with an object by calling the methods it needs, rather than checking whether the object belongs to one exact class. This is duck typing: if an object supports the required behavior, it can be used. A file-processing function that loops over lines, for example, can accept a real file, an in-memory stream, or another object that supplies iteration.

Try the operation first. The book calls this EAFP, or Easier to Ask Forgiveness than Permission: attempt the operation, then handle relevant exceptions. That is often clearer and more flexible than repeatedly checking types. But if a sequence of operations could partly alter an object before failing, check required methods or capabilities first. Polymorphism does not excuse ignoring partial changes or genuine errors.

Analysis

This approach anticipates what modern interface design calls structural typing: compatibility comes from a useful contract, not a shared family tree. It encourages components to be reused in contexts their original authors never anticipated. The important nuance is that a successful first operation does not guarantee that every later operation will succeed. Where changes must be atomic, lightweight preflight checks or a transactional design can matter more than stylistic purity.

Stream data through iterators instead of loading every record at once

Consume one piece at a time. Iterators let a program process an input incrementally. For an ordinary text file, looping directly over the file yields one line at a time, avoiding the memory cost of reading the whole file into a list. For large binary input, a generator can yield fixed-size chunks. The same pattern applies to database rows and network data.

Build only what you need. A generator pauses at each yield and resumes when the next value is requested. That makes it useful for long or potentially unbounded sequences, and it lets downstream code stop early. For strings, however, repeatedly concatenating immutable pieces can be costly. Collect pieces and join them once, or stream them onward when a complete string is unnecessary.

Analysis

Streaming connects a simple language feature to a broader systems principle: apply backpressure by producing data only as fast as consumers need it. This can reduce memory use and improve responsiveness, especially in file and network pipelines. Laziness is not automatically faster, however, and it can make resource lifetimes and error timing less obvious. The sound choice depends on whether the application needs the whole result, can stop early, or must retain data for later access.

Make boundaries explicit: bytes, encodings, time zones, and money

State the representation. A byte string is not automatically meaningful text. Unicode represents characters, while encodings specify how those characters become bytes. The book's practical rule is to decode external text when it enters a program and encode it when it leaves. Likewise, files should be opened in text or binary mode deliberately, because some platforms translate line endings in text mode.

Choose the right arithmetic. The datetime tools make durations explicit with timedelta objects, while time-zone conversion requires suitable time-zone information. For money, Python 2.4's Decimal type avoids binary floating-point representation errors in decimal calculations. It is slower than float, so it is not a universal replacement. The point is to make the boundary and precision requirements explicit instead of relying on a guess.

Analysis

These examples share a deeper lesson: bugs often appear where one representation crosses into another. Explicit conversion at system boundaries is a form of risk control, much like validating units in engineering or specifying schemas in data exchange. The book is right to resist automatic guessing, particularly for encodings and time zones. Its specific APIs are historically dated, but the distinction between bytes and characters, or exact decimal values and binary approximations, remains essential.

Choose heaps, sets, and sorting keys for the operation you repeat

Match structure to workload. Repeated membership tests on a list scan its elements; a set or dictionary is usually better when values are hashable. A heap keeps the smallest item readily available while additions and removals take time proportional to the logarithm of the collection's size. For a sorted list, bisect supports faster searches than a full scan.

Sort by a useful key. The book teaches decorate-sort-undecorate, or DSU: pair each item with a comparison key, sort those pairs, then recover the original items. This handles cases such as case-insensitive strings or filenames with embedded numbers. Python 2.4 adds direct key-based sorting, which makes the same idea simpler. Benchmark the actual workload before choosing a clever alternative.

Analysis

The practical insight is not merely that one data structure is faster than another. It is that performance depends on the operation pattern: lookup-heavy workloads favor hashing, repeated minimum extraction favors heaps, and nearly ordered data can make sorting unusually efficient. The book's repeated emphasis on measurement is sound because machine, data, and implementation details change outcomes. Complexity analysis narrows the candidates; measurement decides which candidate earns its keep.

Use Python's object model to remove boilerplate, not to show off

Delegate before duplicating. Python's object model lets a class customize attribute access, wrap methods, and reuse behavior through inheritance or delegation. Properties can manage attribute access without repetitive accessor methods. The built-in super supports cooperative calls across inheritance hierarchies. Descriptors and metaclasses offer still deeper control over attributes and class creation.

Keep the mechanism proportional. The book illustrates patterns such as Borg, where separate instances share state, and the Null Object, which safely does nothing in place of a missing object. It also warns that a module may be simpler than a Singleton class, and that advanced machinery should be used only when it solves a real problem. A concise interface is valuable; hidden behavior that surprises maintainers is not.

Analysis

The strongest object-oriented advice here is restraint. Dynamic dispatch and class customization can remove genuine boilerplate, but they can also make behavior harder to locate. Delegation is especially useful when inheritance would impose a false taxonomy or expose methods that should remain hidden. The useful test is whether the abstraction clarifies a stable relationship. If a module or an ordinary function communicates the design more directly, the more elaborate pattern has no special claim to elegance.

Use pickle for Python objects, databases for searchable shared records

Pick the persistence model deliberately. The marshal module is fast but limited to basic Python objects and does not promise compatibility across Python versions. Pickle and cPickle handle richer Python objects, including instances, but save classes and functions by name rather than saving their code. The shelve module offers a dictionary-like persistent store, though mutations to retrieved mutable objects must be written back unless writeback is enabled.

Use databases for data operations. Relational databases provide querying and structured access through Python's DB API, while Berkeley DB offers simpler key-value storage. Serialized Python objects can be stored inside database fields when appropriate, but that does not make them searchable as structured data. Never unpickle data from an untrusted source: the operation can execute dangerous behavior.

Analysis

The distinction between serialization and databases is a useful architectural boundary. Serialization preserves an object's shape for another Python process; a database organizes records for selection, querying, and shared access. Mixing the two can be effective, but it does not grant the serialized payload the database's queryability. The security warning is crucial: convenience formats are not automatically safe formats. Persistence choices should account for trust, long-term compatibility, and the kinds of questions the application must ask of its data.

Make failures visible, then automate checks that stop regressions

Expose enough evidence. The debugging recipes show how to record tracebacks, inspect local variables, monitor garbage collection, and add trace messages to programs that cannot use an interactive debugger. The testing recipes use unittest and doctest, including Python 2.4's ability to run examples from separate text files as tests. Assertions can catch violated assumptions during development.

Repeat the checks. A test runner can execute a module's tests automatically, while doctests can ensure that documented examples still work. For numerical code, ordinary equality tests may be too strict; interval checks can verify that results lie within an acceptable tolerance. Remember that optimized Python runs can remove assert statements, so assertions are not a replacement for a dedicated test suite.

Analysis

This chapter treats testing as part of construction, not a ceremonial phase after construction. That fits modern continuous integration, where small checks run repeatedly and failures arrive close to the change that caused them. The recipes also show why observability matters: a traceback with local values can turn a mysterious failure into a specific bad input. Still, tests and logs reveal only the cases they cover. Concurrency, external services, and malformed inputs need deliberate test design rather than confidence by volume.

Coordinate threads with queues, and reserve threads for work they simplify

Communicate through queues. Queue.Queue is the book's favored tool for coordinating producer and worker threads because it handles synchronization internally. A thread pool can take work requests from a queue, process them, and return results through another queue. Sentinel values can tell consumer threads to stop cleanly. Python does not safely kill arbitrary threads, so threads should be asked to finish and given a chance to clean up.

Know what threads buy. In CPython, the Global Interpreter Lock limits parallel execution of pure Python code across multiple processors. Threads can still help with blocking I/O and responsive interfaces. For some applications, cooperative generators or event-driven frameworks such as Twisted and asyncore are better fits. Keep shared state small, and protect any shared mutable data that remains.

Analysis

The useful distinction is between concurrency for responsiveness and parallelism for computation. A thread that waits for a socket can free the main interface to respond, even if pure Python CPU work does not scale across cores under CPython's lock. Message passing through queues narrows the number of places where shared-state bugs can occur. The tradeoff is that asynchronous systems bring their own control-flow complexity, so they are not automatically simpler than threads for every application.

Let protocol libraries handle network machinery, then focus on your application

Build on established layers. The recipes use standard modules for HTTP, FTP, email, and sockets, and higher-level tools such as XML-RPC and Twisted for distributed work. XML parsing can use SAX for event-driven processing or the DOM when a whole document tree is useful. Other recipes show CGI scripts, RSS aggregation, and extracting data from Microsoft formats through platform-specific interfaces.

Reuse the system you have. Tkinter, wxPython, Jython, and Windows extensions appear alongside Unix tools, reflecting Python's reach across platforms. System-administration examples use Python to inspect logs, automate file work, or query operating-system facilities. The recurring tactic is to wrap an existing protocol or service rather than reimplement its internals, while keeping platform-specific assumptions visible.

Analysis

This collection's breadth is itself a lesson: Python often serves as connective tissue between protocols, operating systems, databases, and user interfaces. The best leverage comes from choosing the highest-level reliable interface that still exposes the control the task requires. That also limits the amount of machinery a team must maintain. The caution is that wrappers inherit the security and compatibility assumptions of the systems beneath them, so simplicity at the Python layer should not be mistaken for simplicity of risk.

Analysis

Python Cookbook, Second Edition is best understood as a working reference built from community practice, not as a linear course or a single thesis-driven argument. Its 20 chapters move from text and files through algorithms, object-oriented programming, persistence, testing, concurrency, system administration, interfaces, XML, networking, and program construction. The recipe format makes the book useful by lookup: each entry starts from a concrete problem, offers an implementable approach, and explains the tradeoffs that are easy to miss when copying code.

The book's strongest unifying idea is that Python's value comes from composition. A small function, a standard-library module, a protocol wrapper, or a generator can often replace a larger custom mechanism. The chapters repeatedly show how polymorphism, iterators, dictionaries, and introspection make those pieces adaptable. They also model a practical engineering discipline: choose the simplest approach that meets the need, but measure when performance matters, and preserve enough diagnostic information to understand failures.

There is productive tension between simplicity and power. The book celebrates elegant shortcuts, yet its strongest examples often warn against cleverness, premature optimization, unchecked shared state, and unnecessary metaprogramming. That tension is not a flaw; it is the central judgment readers must learn. A terse expression may be perfect in a local transformation and terrible as a maintenance interface. A generic abstraction can eliminate duplication, but only if its added machinery remains easier to understand than the problem it solves.

The edition's code targets Python 2.3 and 2.4, which makes many APIs and idioms historically specific. Its durable value lies less in copying snippets unchanged than in learning how to reason about boundaries, interfaces, iteration, state, and failure. Readers should treat the book as a catalog of problem-solving patterns, verify current library behavior, and carry forward the design judgment rather than the syntax alone.

Summary in 30 Seconds
Treat code as small, composable pieces: a generator, a standard-library module, or a protocol wrapper often beats a custom mechanism. Program to behavior rather than exact types, and handle exceptions. Stream data lazily, decode text at the boundary, and use Decimal for money. Choose structures for repeated operations and benchmark first. Coordinate threads with queues, test automatically, and avoid cleverness that surprises maintainers.
Contains spoilers

Last updated:

Report Issue

Review Summary

4.18 out of 5
Average of 1k+ ratings from Goodreads and Amazon.

Python Cookbook receives mostly positive reviews, with readers praising its comprehensive coverage of Python 3 and practical recipes. Many find it useful for improving their coding skills and learning advanced techniques. The book is considered a valuable reference for experienced programmers, offering elegant solutions to common problems. Some criticisms include outdated content and the assumption of prior Python knowledge. Reviewers appreciate the book's structure, clear explanations, and focus on idiomatic Python. Overall, it's highly recommended for intermediate to advanced Python developers looking to enhance their skills.

Your rating:
4.54
110 ratings
Want to read the full book?

Glossary

Easier to Ask Forgiveness than Permission (EAFP)

Try operations, handle exceptions

A Python programming style that attempts the operation needed and handles an exception if it fails, rather than checking beforehand whether the operation appears safe. It supports flexible behavior across different object types. The cookbook cautions that this approach can leave partial changes if a later operation fails, so preflight checks may be preferable when a sequence of mutations must remain intact.

duck typing

Compatibility by supported behavior

A style of polymorphism in which code relies on the operations an object supports rather than requiring the object to belong to a specific class. A function that needs iteration can accept any object that provides it, whether it is a file, a list, or a custom stream. The approach encourages reusable code, but callers still need to handle missing or failing operations.

generator

An iterator that yields lazily

A function or expression that produces values one at a time rather than constructing the entire result immediately. A function containing yield pauses at each yielded value and resumes when the next value is requested, retaining its local state. Generators are useful for large files, streams, and potentially unbounded sequences because they avoid storing all results in memory.

decorate-sort-undecorate (DSU)

Sort items through temporary keys

A sorting technique that first pairs each item with a key used for comparison, sorts those pairs, and then extracts the original items. It supports custom ordering, such as sorting strings without regard to case or filenames by embedded numbers. Python 2.4's key argument provides built-in support for the same general operation.

Global Interpreter Lock (GIL)

CPython's bytecode execution lock

A lock in the conventional C implementation of Python that protects interpreter data structures by allowing only one thread at a time to execute Python bytecode. Blocking I/O releases the lock, so threads can still improve responsiveness or overlap waiting. The cookbook notes that pure Python threads generally do not provide parallel CPU execution across multiple processors.

Py-DBAPI

Common Python database interface

The Python Database API specification provides a shared interface for connecting to database modules, creating cursors, executing statements, and fetching results. It allows application code to work across different database engines with limited changes. The modules may still differ in parameter-passing style and in details of their underlying database features.

SAX

Event-driven XML parsing

A parser interface that reports XML content as events, such as the start or end of an element and runs of character data. Applications handle these events without necessarily storing the entire document in memory. SAX suits streaming and simple extraction tasks, but an application must maintain any structure it needs during parsing.

Document Object Model (DOM)

XML represented as a node tree

A programming interface that represents a document as a tree of nodes, including elements, attributes, and text. Code can navigate and modify the tree after parsing. DOM is convenient when processing requires access to different parts of a document or structural relationships, but holding the full tree in memory can be costly for large documents.

Borg

Instances share one state

A Python idiom that allows multiple instances of a class while making them share the same instance dictionary. The instances remain distinct objects, but changes to their shared attributes are visible through every instance. It can address needs often associated with a Singleton, while focusing on shared state rather than enforcing a single object identity.

Null Object Design Pattern

A harmless do-nothing substitute

A design pattern that replaces a special missing value with an object whose operations safely do nothing or return itself. This can remove repeated checks for None and simplify polymorphic code. The cookbook also notes a risk: a Null object can hide an unexpected missing value, allowing a bug to surface later and farther from its cause.

descriptor

Customizes attribute access

An object that defines special methods such as get, set, or delete to control how another object handles an attribute. Python's property is a familiar descriptor. Descriptors can implement computed, cached, aliased, or restricted attributes, and they are one of the mechanisms behind Python's object model.

metaclass

A class that constructs classes

The type of a class object, invoked when a class statement creates a new class. A custom metaclass can inspect or modify the class name, bases, and namespace during construction. This enables class-wide behavior such as automatic method generation or validation, but adds complexity and is best reserved for needs that simpler functions, descriptors, or inheritance cannot meet.

About the Author

Alex Martelli is an Italian-born software developer and technical writer. He is best known for his contributions to the Python programming language and community. Martelli has authored several books on Python, including the popular "Python in a Nutshell" and "Python Cookbook." He has also been a prolific contributor to online Python forums and mailing lists, sharing his expertise and helping others learn the language. Martelli worked for Google as a Senior Staff Engineer and is recognized as a Python Software Foundation Fellow. His writing style is known for being clear, concise, and practical, making complex concepts accessible to readers.

Download PDF

To save this Python Cookbook summary for later, download the free PDF. You can print it out, or read offline at your convenience.
Download PDF
File size: 0.35 MB     Pages: 17

Download EPUB

To read this Python Cookbook summary on your e-reader device or app, download the free EPUB. The .epub digital book format is ideal for reading ebooks on phones, tablets, and e-readers.
Download EPUB
File size: 1.43 MB     Pages: 20
Want to read the full book?
Follow
Listen
Home
Swipe
Library
Get App
Try Full Access for 3 Days
Listen, bookmark, and more
Compare Features Free Pro
📖 Read Summaries
Read unlimited summaries. Free users get 3 per month
🎧 Listen to Summaries
Listen to unlimited summaries in 40 languages
—
❤️ Unlimited Bookmarks
Free users are limited to 4
—
📜 Unlimited History
Free users are limited to 4
—
📥 Unlimited Downloads
Free users are limited to 1
—
Risk-Free Timeline
Today: Get Instant Access
Listen to full summaries of 26,000+ books. That's 12,000+ hours of audio!
Day 2: Trial Reminder
We'll send you a notification that your trial is ending soon.
Day 3: Your subscription begins
You'll be charged on Nov 24,
cancel anytime before.
Consume 2.8× More Books
2.8× more books Listening Reading
Our users love us
600,000+ readers
Trustpilot Rating
TrustPilot
4.6 Excellent
This site is a total game-changer. I've been flying through book summaries like never before. Highly, highly recommend.
— Dave G
Worth my money and time, and really well made. I've never seen this quality of summaries on other websites. Very helpful!
— Em
Highly recommended!! Fantastic service. Perfect for those that want a little more than a teaser but not all the intricate details of a full audio book.
— Greg M
Save 62%
Yearly
$119.88 $44.99/year/yr
$3.75/mo
Monthly
$9.99/mo
Start a 3-Day Free Trial
3 days free, then $44.99/year. Cancel anytime.
Unlock a world of fiction & nonfiction books
26,000+ books for the price of 2 books
Read any book in 10 minutes
Discover new books like Tinder
Request any book if it's not summarized
Read more books than anyone you know
#1 app for book lovers
Lifelike & immersive summaries
30-day money-back guarantee
Download summaries in EPUBs or PDFs
Cancel anytime in a few clicks

Why one summary?

Scanner
Find a barcode to scan

⭐
⭐
⭐
We have a special gift for you
Open
38% off
DISCOUNT FOR YOU
⭐
⭐
⭐
$79.99
$49.99/year
Only $4.16 per month
Continue
2 taps to start, super easy to cancel
Settings
General
Widget
Loading...