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.
Buy the Book PDF Buy Kindle Edition
Also available in translations: 日本語, Deutsche, 简体中文, 繁体中文, Polszczyzna, 한국어, Português
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.
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.
bytes
, str
, and unicode
start
, end
, and stride
in a Single Slicemap
and filter
enumerate
Over rangezip
to Process Iterators in Parallelelse
Blocks After for
and while
Loopstry
/except
/else
/finally
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.
None
None
and Docstrings to Specify Dynamic Default ArgumentsPython 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.
@classmethod
Polymorphism to Construct Objects Genericallysuper
collections.abc
for Custom Container TypesMetaclasses 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.
@property
Instead of Refactoring Attributes@property
Methods__getattr__
, __getattribute__
, and __setattr__
for Lazy AttributesPython 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.
subprocess
to Manage Child ProcessesLock
to Prevent Data Races in ThreadsQueue
to Coordinate Work Between Threadsconcurrent.futures
for True ParallelismPython 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.
functools.wraps
contextlib
and with
Statements for Reusable try
/finally
Behaviorpickle
Reliable with copyreg
datetime
Instead of time
for Local Clocksdecimal
When Precision is ParamountCollaborating 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.
Exception
to Insulate Callers from APIsPython 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.
repr
Strings for Debugging Outputunittest
pdb
tracemalloc
to Understand Memory Usage and LeaksThe publishing house Novatec Editora has translated and released a Portuguese version of Effective Python. You can buy the book directly from the publisher.
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.
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.
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.
The publishing house Helion has translated and released a Polish version of Effective Python. You can buy the book directly from the publisher.
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).
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.
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.
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.
I gave a talk at PyCon Montréal entitled “How to Be More Effective with Functions”. Continue reading »
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.
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.
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 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 »
When a function takes a list of objects as a parameter, it’s often important to iterate over that list multiple times. Continue reading »
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.
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.
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.
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.