Preface

The Python programming language has unique strengths and charms that can be hard to grasp. Many programmers familiar with other languages approach Python from a limited mindset instead of embracing its full capabilities. Some programmers go too far in the other direction, overusing Python features that can cause big problems later.

This book provides insight into the Pythonic way of writing programs: the best way to use Python. It builds on a fundamental understanding of the language that I assume you already have. Novice programmers will learn the best practices of Python’s critical features. Experienced programmers will learn how to embrace a new tool with confidence.

With this book I hope to help you use Python to accomplish your goals, whatever they may be, or at least to help you have more fun on your journey with programming.

What This Book Covers

Each chapter in this book contains a broad but related set of items. Feel free to jump between items and follow your interest. Each item contains concise and specific guidance explaining how you can write Python programs more effectively. Items include advice on what to do, what to avoid, how to strike the right balance, and why this is the best choice. Items reference each other to make it easier to fill in the gaps as you read.

This third edition covers the language up through Python version 3.13 (see Item 1: “Know Which Version of Python You’re Using”). This book includes 35 completely new items compared to the second edition. Most of the items from the second edition have been revised and included, but many have undergone substantial updates. For some items, my advice has completely changed due to best practices evolving as Python has matured over the past five years.

Python takes a “batteries included” approach to its standard library. Many of these built-in packages are so closely intertwined with idiomatic Python that they may as well be part of the language specification. The full set of standard modules is too large to cover in this book, but I’ve included the ones that I feel are critical to be aware of and use.

Python also has a vibrant ecosystem of community-built modules that extend the language in valuable ways. Although I mention important packages to know about in various items, this book is not intended to be a thorough reference. Similarly, despite the importance of Python package management, I avoid going into the details about it because it’s rapidly changing and evolving.

Chapter 1: Pythonic Thinking

The Python community has come to use the adjective Pythonic to describe code that follows a particular style. The idioms of Python have emerged over time through experience using the language and collaborating with other programmers. This chapter covers the best ways to do the most common things in Python.

Chapter 2: Strings and Slicing

Python has built-in syntax, methods, and modules for string and sequence processing. These capabilities are so essential that you’ll see them in nearly every program. They make Python an excellent language for parsing text, inspecting data formats, and interfacing with the low-level binary representations used by computers.

Chapter 3: Loops and Iterators

Processing through sequential data is a critical need in programs. Loops in Python feel natural and capable for the most common tasks involving built-in data types, container types, and user-defined classes. Python also supports iterators, which enable a more functional-style approach to processing arbitrary streams of data with significant benefits.

Chapter 4: Dictionaries

Python’s built-in dictionary type is a versatile data structure for bookkeeping in programs. Compared to simple lists, dictionaries provide much better performance for adding and removing items. Python also has special syntax and related built-in modules that enhance dictionaries beyond what you might expect from hash tables in other languages.

Chapter 5: Functions

Functions in Python have a variety of extra features that can make a programmer’s life easier. Some are similar to capabilities in other programming languages, but many are unique to Python. This chapter covers how to use functions to clarify intention, promote reuse, and reduce bugs.

Chapter 6: Comprehensions and Generators

Python has special syntax for quickly iterating through lists, dictionaries, and sets to generate derivative data structures. It also allows for a stream of iterable values to be incrementally returned by a function. This chapter covers how these features can provide better performance, reduced memory usage, and improved readability.

Chapter 7: Classes and Interfaces

Python is an object-oriented language. Getting things done in Python often requires writing new classes and defining how they interact through their interfaces and hierarchies. This chapter covers how to use classes to express intended behaviors with objects.

Chapter 8: Metaclasses and Attributes

Metaclasses and dynamic attributes are powerful Python features. However, they also enable you to implement extremely bizarre and unexpected behaviors. This chapter covers the common idioms for using these mechanisms to ensure that you follow the rule of least surprise.

Chapter 9: Concurrency and Parallelism

With features such as threads and asynchronous coroutines, Python makes it easy to write concurrent programs that do many different things seemingly at the same time. Python can also be used to do parallel work through system calls, subprocesses, and special modules. This chapter covers how to best utilize Python in these subtly different situations.

Chapter 10: Robustness

Making programs dependable when they encounter unexpected circumstances is just as important as making programs with correct functionality. Python has built-in features and modules that aid in hardening your programs so they are robust in a wide variety of situations.

Chapter 11: Performance

Python includes a variety of capabilities that enable programs to achieve surprisingly impressive performance with relatively low amounts of effort. Using these features, it’s possible to extract maximum performance from a host system while retaining the productivity gains afforded by Python’s high-level nature.

Chapter 12: Data Structures and Algorithms

Python includes optimized implementations of many standard data structures and algorithms that can help you achieve high performance with minimal effort. The language also provides battle-tested data types and helper functions for common tasks (e.g., working with currency and time) that allow you focus on your program’s core requirements.

Chapter 13: Testing and Debugging

You should always test your code, regardless of what language it’s written in, but with Python, testing is especially important. Python’s dynamic features can increase the risk of runtime errors in unique ways. Luckily, they also make it easier to write tests and diagnose malfunctioning programs. This chapter covers Python’s built-in tools for testing and debugging.

Chapter 14: Collaboration

Collaborating on Python programs requires you to be deliberate about how you write your code. Even if you’re working alone, you’ll want to understand how to use modules written by others. This chapter covers the standard tools and best practices that enable people to work together on Python programs.

Conventions Used in This Book

Python code snippets in this book are in monospace font and have syntax highlighting. When lines are long, I use ➥ characters to show when they wrap. I truncate some snippets with ellipses (...) to indicate regions where code exists that isn’t essential for expressing the point. You’ll need to download the full example code (see below on where to get it) in order to get these truncated snippets to run correctly on your computer.

I take some artistic license with the Python style guide in order to make the code examples better fit the format of a book or to highlight the most important parts. I’ve also left out embedded documentation to reduce the size of code examples. I strongly suggest that you don’t emulate this in your projects; instead, you should follow the style guide (see Item 2: “Follow the PEP 8 Style Guide”) and write documentation (see Item 118: “Write Docstrings for Every Function, Class, and Module”).

Most code snippets in this book are accompanied by the corresponding output from running the code. When I say “output,” I mean console or terminal output: what you see when running the Python program in an interactive interpreter. Output sections are in monospace font, and each is preceded by a >>> line (the Python interactive prompt). The idea is that you could type the code snippets into a Python shell and reproduce the expected output.

Finally, there are some other sections in monospace font that are not preceded by a >>> line. These represent the output of running programs besides the Python interpreter. These examples often begin with $ characters to indicate that I’m running programs from a command-line shell like Bash. If you’re running these commands on Windows or another type of system, you may need to adjust the program names and arguments accordingly.

Where to Get the Code and Errata

It’s useful to view many of the examples from this book as whole programs without interleaved prose. This also gives you a chance to tinker with the code yourself and understand why the program works as described. You can find the source code for all code snippets in this book on the book’s website, https://effectivepython.com. The website also provides instructions on how to report errors. Thank you in advance for contacting me about any errors you find.

Register your copy of Effective Python: 125 Specific Ways to Write Better Python, 3rd Edition on the InformIT site for convenient access to updates and/or corrections as they become available. To start the registration process, go to informit.com/register and log in or create an account. Enter the product ISBN (9780138172183) and click Submit. If you would like to be notified of exclusive offers on new editions and updates, please check the box to receive email from us.