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.
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.
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.
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.
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.
stridein a Single Slice
zipto Process Iterators in Parallel
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.
Noneand Docstrings to Specify Dynamic Default Arguments
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.
@classmethodPolymorphism to Construct Objects Generically
collections.abcfor Custom Container Types
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.
@propertyInstead of Refactoring Attributes
__setattr__for Lazy Attributes
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.
subprocessto Manage Child Processes
Lockto Prevent Data Races in Threads
Queueto Coordinate Work Between Threads
concurrent.futuresfor True Parallelism
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.
withStatements for Reusable
timefor Local Clocks
decimalWhen Precision is Paramount
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.
Exceptionto Insulate Callers from APIs
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.
reprStrings for Debugging Output
tracemallocto Understand Memory Usage and Leaks
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.
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 »
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.
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 »
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 »
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
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 »
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 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.