The Book

Preface

Effective Python Book Cover

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

Effective Python 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 capabilities. Experienced programmers will learn how to embrace the strangeness of a new tool with confidence.

My goal is to prepare you to make a big impact with Python.

Now Available

Print and digital editions of the book are now available! Effective Python was published by Addison-Wesley (an imprint of Pearson) on March 6th, 2015.

Buy the Book Buy ePub / PDF Buy Kindle Edition

Also available in translations: 日本語, Deutsche, 简体中文, 繁体中文, Polszczyzna, 한국어, Português

Already Have the Book?

Visit the GitHub project to see all of the code snippets from the book in one place. Run and modify the example code yourself to confirm your understanding. You can also report any errors you’ve found or view the list of corrections.

For future updates about the book, related videos, translations, conference presentations, and more, choose one of these ways to stay in touch.

@

More Info

What This Book Covers

Each chapter in Effective Python contains a broad but related set of items. You should 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.

The items in this book are for Python 3 and Python 2 programmers alike. Programmers using alternative runtimes like Jython, IronPython, or PyPy should also find the majority of items to be applicable.

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 working with others. This chapter covers the best way to do the most common things in Python.

  1. Know Which Version of Python You’re Using
  2. Follow the PEP 8 Style Guide
  3. Know the Differences Between bytes, str, and unicode
  4. Write Helper Functions Instead of Complex Expressions
  5. Know How to Slice Sequences
  6. Avoid Using start, end, and stride in a Single Slice
  7. Use List Comprehensions Instead of map and filter
  8. Avoid More Than Two Expressions in List Comprehensions
  9. Consider Generator Expressions for Large Comprehensions
  10. Prefer enumerate Over range
  11. Use zip to Process Iterators in Parallel
  12. Avoid else Blocks After for and while Loops
  13. Take Advantage of Each Block in try/except/else/finally

Chapter 2: Functions

Functions in Python have a variety of extra features that 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.

  1. Prefer Exceptions to Returning None
  2. Know How Closures Interact with Variable Scope
  3. Consider Generators Instead of Returning Lists
  4. Be Defensive When Iterating Over Arguments
  5. Reduce Visual Noise with Variable Positional Arguments
  6. Provide Optional Behavior with Keyword Arguments
  7. Use None and Docstrings to Specify Dynamic Default Arguments
  8. Enforce Clarity with Keyword-Only Arguments

Chapter 3: Classes and Inheritance

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 and inheritance to express your intended behaviors with objects.

  1. Prefer Helper Classes Over Bookkeeping with Dictionaries and Tuples
  2. Accept Functions for Simple Interfaces Instead of Classes
  3. Use @classmethod Polymorphism to Construct Objects Generically
  4. Initialize Parent Classes with super
  5. Use Multiple Inheritance Only for Mix-in Utility Classes
  6. Prefer Public Attributes Over Private Ones
  7. Inherit from collections.abc for Custom Container Types

Chapter 4: 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.

  1. Use Plain Attributes Instead of Get and Set Methods
  2. Consider @property Instead of Refactoring Attributes
  3. Use Descriptors for Reusable @property Methods
  4. Use __getattr__, __getattribute__, and __setattr__ for Lazy Attributes
  5. Validate Subclasses with Metaclasses
  6. Register Class Existence with Metaclasses
  7. Annotate Class Attributes with Metaclasses

Chapter 5: Concurrency and Parallelism

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 C-extensions. This chapter covers how to best utilize Python in these subtly different situations.

  1. Use subprocess to Manage Child Processes
  2. Use Threads for Blocking I/O, Avoid for Parallelism
  3. Use Lock to Prevent Data Races in Threads
  4. Use Queue to Coordinate Work Between Threads
  5. Consider Coroutines to Run Many Functions Concurrently
  6. Consider concurrent.futures for True Parallelism

Chapter 6: Built-in Modules

Python is installed with many of the important modules that you’ll need to write programs. These standard packages are so closely intertwined with idiomatic Python that they may as well be part of the language specification. This chapter covers the essential built-in modules.

  1. Define Function Decorators with functools.wraps
  2. Consider contextlib and with Statements for Reusable try/finally Behavior
  3. Make pickle Reliable with copyreg
  4. Use datetime Instead of time for Local Clocks
  5. Use Built-in Algorithms and Data Structures
  6. Use decimal When Precision is Paramount
  7. Know Where to Find Community-Built Modules

Chapter 7: 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.

  1. Write Docstrings for Every Function, Class, and Module
  2. Use Packages to Organize Modules and Provide Stable APIs
  3. Define a Root Exception to Insulate Callers from APIs
  4. Know How to Break Circular Dependencies
  5. Use Virtual Environments for Isolated and Reproducible Dependencies

Chapter 8: Production

Python has facilities for adapting to multiple deployment environments. It also has built-in modules that aid in hardening your programs and making them bulletproof. This chapter covers how to use Python to debug, optimize, and test your programs to maximize quality and performance at runtime.

  1. Consider Module-Scoped Code to Configure Deployment Environments
  2. Use repr Strings for Debugging Output
  3. Test Everything with unittest
  4. Consider Interactive Debugging with pdb
  5. Profile Before Optimizing
  6. Use tracemalloc to Understand Memory Usage and Leaks

About the Author

Brett Slatkin

Brett Slatkin

Brett is a Senior Staff Software Engineer at Google. He is the engineering lead and co-founder of Google Consumer Surveys. He formerly worked on Google App Engine’s Python infrastructure. He is the co-creator of the PubSubHubbub protocol. Nine years ago he cut his teeth using Python to manage Google’s enormous fleet of servers.

Outside of his day job, he works on open source tools and writes about software, bicycles, and other topics on his personal website. He earned his B.S. in Computer Engineering from Columbia University in the City of New York. He lives in San Francisco.

Updates

Python Eficaz

Sat 06 August 2016

Python Eficaz

The publishing house Novatec Editora has translated and released a Portuguese version of Effective Python. You can buy the book directly from the publisher.

Effective Python 简体中文

Sat 07 May 2016

Effective Python 简体中文

The publishing house 机械工业出版社 (China Machine Press) has translated and released a Chinese (Simplified) version of Effective Python. You can buy the book directly from the publisher or get it on Amazon.cn.

Effective Python 파이썬 코딩의 기술

Sun 03 April 2016

Effective Python 파이썬 코딩의 기술

The publishing house Gilbut Inc. has translated and released a Korean version of Effective Python. The publisher’s website links to many different retailers online where you can buy the book.

Effective Python 日本語

Sat 23 January 2016

Effective Python 日本語

The publishing house O’Reilly Japan has translated and released a Japanese version of Effective Python. You can buy the book directly from the publisher or get it on Amazon.jp.

Efektywny Python

Fri 20 November 2015

Efektywny Python

The publishing house Helion has translated and released a Polish version of Effective Python. You can buy the book directly from the publisher.

Effektiv Python ​programmieren

Sun 15 November 2015

Effektiv Python programmieren

The publishing house mitp-Verlag has translated and released a German version of Effective Python. You can buy the book directly from the publisher or get it on Amazon.de (including Kindle edition).

Talk Python To Me Podcast

Wed 09 September 2015

I was invited on to the Talk Python To Me Podcast to talk about Effective Python. You can read the full transcript here or listen to the audio embedded below. Thanks to Michael Kennedy for being such a welcoming host.

Effective Python 中文版

Thu 27 August 2015

Effective Python 中文版

The publishing house 碁峰 (Acer Peak) has translated and released a Chinese (Traditional) version of Effective Python. You can buy the book directly from the publisher or get a digital edition on Google Play.

Live Lessons Video

Tue 04 August 2015

Effective Python Live Lessons

I worked with Addison-Wesley to produce a video version of the book Effective Python. You can view samples and buy the video on the publisher’s website.

It includes 5 hours of video, covering 32 items from the book in six lessons. The content is primarily me using a source code editor to write Python programs that demonstrate the items from the book.

Talk at PyCon Montréal

Fri 10 April 2015

I gave a talk at PyCon Montréal entitled “How to Be More Effective with Functions”. Continue reading »

Item 40: Consider Coroutines to Run Many Functions Concurrently

Tue 10 March 2015

Threads give Python programmers a way to run multiple functions seemingly at the same time. But there are three big problems with threads: Continue reading »

The Book Is Published

Fri 06 March 2015

Printed, physical copies of the Effective Python book are now for sale! Follow this link to buy directly from the publisher (free shipping in the USA). The publisher also has ePub and PDF versions available. Follow this link to buy from Amazon. Amazon also has a Kindle edition available.

Digital Editions Now Available

Sun 15 February 2015

Digital editions of Effective Python are now available. Follow this link to buy the ePub or PDF version. Follow this link to buy the Kindle edition. The print copy is due out on March 6th.

Item 23: Accept Functions for Simple Interfaces Instead of Classes

Thu 12 February 2015

Many of Python’s built-in APIs allow you to customize behavior by passing in a function. These hooks are used by APIs to call back your code while they execute. Continue reading »

Item 34: Register Class Existence with Metaclasses

Mon 02 February 2015

A common use of metaclasses is to automatically register types in your program. Registration is useful for doing reverse lookups, where you need to map a simple identifier back to a corresponding class. Continue reading »

Item 53: Use Virtual Environments for Isolated and Reproducible Dependencies

Wed 21 January 2015

Building larger and more complex programs often leads you to rely on various packages from the Python community. You’ll find yourself running pip to install packages like pytz, numpy, and many others.

The problem is that by default pip installs new packages in a global location. That causes all Python programs on your system to be affected by these installed modules. In theory, this shouldn’t be an issue. If you install a package and never import it, how could it affect your programs? Continue reading »

Item 17: Be Defensive When Iterating Over Arguments

Sat 03 January 2015

When a function takes a list of objects as a parameter, it’s often important to iterate over that list multiple times. Continue reading »

The Rough Cut Is Online

Thu 18 December 2014

Can’t wait until next year for the book to be published? The rough cut is now available on Safari Books Online. This is an early preview of the full content of the book before editing has been completed.

Now Available for Preorder

Thu 04 December 2014

Effective Python is now available for preorder on Amazon. Follow this link to buy your copy in advance. It will ship in early 2015 once the book is published.

Final Draft Done

Sun 16 November 2014

The final draft of the book is done. It’s 55,000 words, 250+ pages, 8 chapters, 59 items. Reviewers spent a lot of time looking over earlier drafts to ensure that the book will be useful. Now it’s off to production to be turned into a printable layout.

Welcome

Mon 18 August 2014

This website is now live! Here you’ll find updates about my progress towards Effective Python‘s eventual publishing by Addison-Wesley. You can find other books from the Effective series in Pearson’s online store.