Jump to ratings and reviews
Rate this book

Effective Python: 90 Specific Ways to Write Better Python

Rate this book
Updated and Expanded for Python 3 It’s easy to start developing programs with Python, which is why the language is so popular. However, Python’s unique strengths, charms, and expressiveness can be hard to grasp, and there are hidden pitfalls that can easily trip you up. This second edition of Effective Python will help you master a truly “Pythonic” approach to programming, harnessing Python’s full power to write exceptionally robust and well-performing code. Using the concise, scenario-driven style pioneered in Scott Meyers’ best-selling Effective C++, Brett Slatkin brings together 90 Python best practices, tips, and shortcuts, and explains them with realistic code examples so that you can embrace Python with confidence. Drawing on years of experience building Python infrastructure at Google, Slatkin uncovers little-known quirks and idioms that powerfully impact code behavior and performance. You’ll understand the best way to accomplish key tasks so you can write code that’s easier to understand, maintain, and improve. In addition to even more advice, this new edition substantially revises all items from the first edition to reflect how best practices have evolved. Key features include 30 new actionable guidelines for all major areas of Python Detailed explanations and examples of statements, expressions, and built-in types Best practices for writing functions that clarify intention, promote reuse, and avoid bugs Better techniques and idioms for using comprehensions and generator functions Coverage of how to accurately express behaviors with classes and interfaces Guidance on how to avoid pitfalls with metaclasses and dynamic attributes More efficient and clear approaches to concurrency and parallelism Solutions for optimizing and hardening to maximize performance and quality Techniques and built-in modules that aid in debugging and testing Tools and best practices for collaborative development   Effective Python will prepare growing programmers to make a big impact using Python.

480 pages, Kindle Edition

Published October 25, 2019

332 people are currently reading
905 people want to read

About the author

Ratings & Reviews

What do you think?
Rate this book

Friends & Following

Create a free account to discover what your friends think of this book!

Community Reviews

5 stars
175 (55%)
4 stars
100 (31%)
3 stars
30 (9%)
2 stars
8 (2%)
1 star
0 (0%)
Displaying 1 - 30 of 31 reviews
152 reviews
September 29, 2020
I picked this up after starting to work on a research project 40hrs/week in Python for the first time and wanting to improve my Python. I came to this book comfortable with Python data science basics, implementing methods in Numpy, and with some general data science and development background for R. The goal was to find the 'pit of success' workflows in Python that I use for research and exploratory modeling in R. This didn't quite happen, but I don't think that's quite the goal of the book.

Things that were super useful:

- In-depth explanations of Python's list idioms, and in particular zip() and * for variadic functions
- Explanations of function and class decorators
- General Python best practices recommendations
- Commentary on object-orientation and class design, especially as someone who mostly uses functional patterns

Things that were less useful to me:

- An extension discussion of threaded concurrency rather than true parallelism. The basic tool that I need for data science work is a truly parallel mapping operation, and this didn't really appear in the book in any detail.
- The metaclass chapter, largely because I didn't have enough background to understand the examples.

This book is analogous to Advanced R and has excellent examples, but assumes at least an intermediate level of Python background. I strongly recommend this book to R users (along not as an introduction to the language), as there are definitely design patterns in Python that it would be nice to see integrated in R.

Since my Python background is piecemeal, after reading this book I found myself wanting:

- An in-depth, opinionated guide on how to write doc-strings
- An in-depth, opinionated style-guide, or at least some more basic instructions on how to use a linter
- A brief overview of the OOP landscape in Python
- Discussions of logging and data/argument validation at a beginner level
- Some pointers to libraries like joblib for parallel maps, memoization and memory-mapping, which I've found essential in exploratory and research work.

At several points the book made me wish that I had taken an OS class or programming languages class to have a better understanding of system level stuff or how encapsulated (rather than generic-function) OOP works. This was largely from a personal curiosity point-of-view.

These are all critiques of content because I was personally hoping to learn something not quite in the book's scope; I found the writing itself and examples to be remarkably clear and will recommend and reference this book for years to come.
Profile Image for Vlad Bezden.
233 reviews13 followers
March 1, 2020
I read the first edition of the book and I just finished the second edition. This is one of the best books on Python that I had. Every time I read it I always learn something. I also like the way it organized, 90 items, each one covers specific typic and in the end, it has a summary of the item. It's called "Things to remember". New edition covers asyncio, static analysis, and walrus operator.
If I could I would give this book 10 stars.
Profile Image for Robert Gistvik.
104 reviews1 follower
September 15, 2024
A nicely packaged book with ways to improve your coding. My main concern is the time (and space) spent on examples of how not to do things and that some of the items in the book seems to be "fillers" as they are very basic compared to other examples. I would however recommend this as a dictionary-type book. Skim through the examples and re-iterate a section when you encounter a situation you recognize.
212 reviews10 followers
March 29, 2022
Every book in the *Effective* series is fantastic, and this one is no different. I would go as far as to say that it should be required reading if you develop in Python on a regular basis!

As for the other books in the series, this one is organized into 2-10 page sections with a short bullet-point recommendations at the end. This makes it really easy to skim topics that you are already familiar with, and makes the book useful for everyone from beginners to experienced devs.

The main reason I picked up the book was for its chapter on concurrency and parallelism, which I must say is a bit disappointing in Python. After reading the chapter, I can see why I always found it so confusing: Python has threads and supporting libraries for them, but only one thread can ever run at a time! I thought maybe there was a GIL exception for threads, but apparently not. Threads can be interrupted between any two bytecode instructions, meaning that you have to be careful about sharing data between threads and properly using locks. Since threads are only useful for working around blocking IO, such as network calls and file reads/writes, the newer asyncio machinery, where the programmer determines the interruption points, is a much more principled approach to concurrency. The author points out, though, that it's easy to accidentally use blocking IO operations, and even demonstrates how to debug your program to find slow-downs due to blocking. Why doesn't Python have async versions of open, etc.?
Profile Image for Franz Scherer.
76 reviews8 followers
October 5, 2021
Sehr ausnahmsweise möchte ich ein Buch bewerten das ich gar nicht ganz gelesen habe, weil es hier um ein Buch geht das vielleicht auch andere, so wie ich, vielleicht gar nicht von Deckel zu Deckel lesen. In 90 sehr präzisen, gut erklärten und knapp formulierten Tipps wird einem hier gesagt "Mach diese eine Sache doch vielleicht lieber so und so". Das für mich bisher mit Abstand wichtigste Programmier-Buch, mein Code ist durch dieses Buch sicherlich besser geworden. Ratschläge die man nicht brauch kann man bedenkenlos überspringen ohne in anderen Ratschlägen dadurch etwas nicht zu verstehen, die Kapitel sind sinnvolle Einheiten, ich bin sehr zufrieden.
Profile Image for João.
64 reviews
December 27, 2022
It's a nice book. It compiles a lot of good practices when writing Python code. I've found a lot of things I regularly do, and others that I will definitely start doing.

However the book gets a bit tedious at a certain point. In addition, the book avoids auxiliary pictures or schematics completely, and I feel a lot of concepts could have benefited greatly from a visual component. The text gets a bit too involved in these more complex topics.
1 review
February 9, 2024
'Effective Python' is dedicated to programmers already familiar with
Python. The book shows good and modern Python practices and new
language features. Similarly to 'Effective C++' and 'Effective Java'
from the same series, 'Effective Python' consists of independent items
(90 in this case), each item introducing one recommended practice.

The author tries to use real problems to demonstrate the
concepts. This has drawbacks. Many items have separate examples, so
explaining the examples takes quite a lot of space. Understanding what
the example is trying to accomplish sometimes takes more brain power
that grasping the Python feature that the item is presenting. The
examples still feel pretty artificial, a program for processing
incoming emails would be quite complex in real life, so the book
introduces some simplistic version of a part of such a program. I
would personally prefer these examples to be completely artificial,
like do_some_heavy_computation() or do_some_blocking_io() which much
less space dedicated to the explanation of the artificial problem
domain.

Many items introduce useful language features which are not needed
frequently in day-to-day projects. For example, meta classes related
features are very useful for libraries such as ORMs, but many projects
will not see the need for using these mechanism often or at all. To
better retain knowledge of these powerful, but not often needed
features I made a notes with key items that were new to me and plan to
review these notes regularly. Also, when reading the book, for many
items I was making commits to a Python project that I currently work
on. The commits introduced recommendations from the items. This
certainly will help to retain knowledge and start using these
recommendations in practice.

In general I found this book very useful for refreshing and
'modernizing' my Python knowledge. It is not as strong as 'Effective
C++' and 'Effective Java' (but these language are also more difficult
with more gotchas), but 'Effective Python' is good enough to strongly
recommend it.
Profile Image for Redowan Delowar.
46 reviews4 followers
July 20, 2023
I love the "effective" style books because of their conciseness and how they don't try to cater to absolute beginners. The author assumes a certain level of expertise from the readers and builds upon their previous knowledge. The book presents 90 real-world problems that you'll encounter in your day-to-day work and provides a single authoritative solution for each "item". Also, it's one of those books that you can skim pretty fast if you already know the most idiomatic way of solving the items you encounter.

One complaint that I have is regarding the concurrency chapter where the problem statement seemed a bit too convoluted and the explanation felt a bit rushed. Plus, the solutions had to be trimmed aggressively to fit in a book format, which hurt the readability a lot. So I had to constantly go to the GitHub repo and check the complete code to understand what was going on. But I picked up a ton of useful tidbits from the parts covering the distinction between bytes, bytearray, and string; what descriptors and metaclasses are all about; how to tap into the subprocess module to pull off some process trickery; and how to best use iterators and generators. Highly recommended if you're already good with your Python-fu and looking for some ideas to add some new tools to your arsenal or sharpen the existing ones.
Profile Image for Yannik.
4 reviews
November 4, 2021
Delivers on the title's promise in spades; it's specific, actionable, and insightful. The 90 items span all the aspects of the language, from functions and classes to testing and collaboration. It's great for learning or reminding yourself about the best approach to specific problems or parts of the language. It also imparts a fair amount of software engineering wisdom through its discussion of the pros and cons of the approaches discussed in each item, especially in later chapters.

The book's an excellent reference, and a big reason for that is the structure of each item. Brett motivates each problem, considers different approaches to solving it, from naive to overwrought; before presenting the best method. Each item is rounded out with a short bulleted list of "Things to Remember". When I've needed a quick reminder I jump to the "Things to Remember" section, and when I've needed to re-learn a Python idiom or approach to development I can re-read the whole item. While the approach is uniform throughout and Brett's writing is very clear, some of the examples were a lot tougher to follow than others. That is to say, the assumed background for different items varies, which is not necessarily a bad thing given the scope and purpose of the book.
Profile Image for Benjamin Manning.
47 reviews8 followers
September 12, 2023
Read this book at the suggestion of Prof John H. - he's my new sensei! An unbelievably good resource to step up coding in python (and coding in general) to the next level. One really needs to be a decent coder already and I also suggest reading while you're working on a project so you get to test the ideas.

I learned so much - reading one of these a night for 90 days, but here's a few of the most important things:

1. Always create effective and comprehensive serialization and deserialization for all classes so you can move things around and checkpoint super fast. I can't tell you the number of times this saved my butt in my current project. John Made me do this before I understood why it's valuable. it's the most valuable.

2. Threading and asyncio are complex, with a high, intimidating barrier to entry, but it's worth it. I was able to cut time on some of my simulations by 100x. It's hard to get all the threads to line up and built into parent classes and also hard to debug, but if you can just make a simple example first and build up slowly, it'll be fine. Also, GPT-4 is very good at turning for loops into correct threads with very little help.

3. Effectively using *args, **kwargs, and args and kwargs in general at first seems simple, but gets complicated fast, but it allows sensational generalizability. Creating code that is totally agnostic to a variety of levels of abstraction is so satisfying, and I got way better at it by really reading and rereading this sections + trying som many examples.
Profile Image for Victor Domingos.
Author 6 books18 followers
September 24, 2020
Full review at: https://no-title.victordomingos.com/a...
---------------------------------------
This is a book that I definitely recommend to anyone who already has some basic or intermediate knowledge of Python. You can read it cover to cover, or just pick the specific topics that you’re interested in at a given time, and get back to it later when you need it. Even more experienced Python users may still benefit from some of the explanations in this book, like those on chapter 6 (“Metaclasses and Attributes”) or on chapter 7 (“Concurrency and Parallelism”).

In a world where the web is full of videos and tutorials on programming (which is great, but not always the most efficient way to learn), it is good to know that there are excellent quality books like this one being published, both on print and on digital formats, which can guide us throughout our journey towards a deeper knowledge of Python and its best programming practices. (...)
1 review2 followers
April 18, 2020
There are a lot of great tips and best practices in the book. The examples are in iterative form, starting from non-Pythonic code and eventually converge into Pythonic code. The author also demonstrate to solve problem in Pythonic way when the use case or scope of the problem grows.

The author includes some really advanced stuffs that you don't normally use or exposed to in your normal day to day usage, such as ways to optimise performance or CPU usage for video streaming across the Internet to handle scalability. Some of them took me a while to gasps the essence.

There are quite a number of mistakes in code indentation which are crucial for understanding context and scope in Python. Few situational examples are not intuitive to understand and follow thoroughly. It is a book worth going through, to aware of the knowledge out there for you to leverage when you need them.
Profile Image for Robert Chang.
60 reviews4 followers
December 9, 2022
I saw Vicki Boykis' recommended this book on Twitter, and it didn't disappoint me. In fact, it is one of the best Python book out there for learning how to write effective Python for intermeidate+ practitioners. I especially appreciate the author's approach to the topics. The writing style was in itself effective - the author always try to solve a problem using plain Python, point out its limitations, and then gradually refactor the code as he introduce new concepts. I find a lot of the tips to be practical and have used quite a few myself during my work! This is definitely a great book for Python practitioners.
Profile Image for Phil Eaton.
117 reviews291 followers
September 27, 2020
The second edition, like the first, is a must-read for professional Python developers. But I wouldn't take all of it that seriously. There are a number of weird, forced suggestions like how fervently he goes into use cases for the walrus operators, generators, and metaclasses specifically. The code ended up overcomplicated and not less brittle than it could have been. In general, just because there's syntax available doesn't mean you should use it for the sake of fellow maintainers (or yourself in the future). But it's good to know what exists.
Profile Image for Alexander Snitsarenko.
41 reviews2 followers
December 31, 2024
A sound effort to list the most interesting features of Python. But, no effort at all to make the book better in terms of presentation of material - no matter how many years one has been with Python, it is impossible to remember all stuff. The author does not include any commenting on code lines, that is completely not Pythonic since this language was built on the principle of clear and natural syntax so that you spend more time on bulding best business logic. Hopefully, this would be improved in its newer version - 125 ways.
252 reviews3 followers
April 11, 2020
An excellent deep dive into (as the title says) specific ways to better use the Python programming language. This does cover most of the questions I've had. If I was a workinig Python programmer, I would want to own this book.

I did find the sections dealing with threading a bit hard to grasp. This probably reflects that the paradigms provided by Python in this area aren't simple for addressing this programming challenge.
1 review
January 21, 2021
The author writes some code and then shows ways to improve the written code. The book is incredible if you can get over this. Roughly half way through the book I started to appreciate the step approach toward more pythonic code.
Profile Image for Ben.
2,727 reviews223 followers
January 18, 2022
This was a really splendid Python book.

I enjoyed learning all the 90 improvements to syntax, refactoring code, and making executions more efficient.

I thought this was a very important coding book and would recommend it to fellow Python developers.

4.8/5
Profile Image for Leo.
315 reviews25 followers
January 7, 2023
Was really useful for me.
Advice for readers - you don't have to read all chapters in full, if topic is familiar to you, or seems too irrelevant - skip to great "Things to remember" recap sections. If you've missed something big - you'll notice that, return and re-read.
Profile Image for Michael.
60 reviews2 followers
December 31, 2021
A good set of rules and conventions that all Python developers should know. The book is already showing some age though and would benefit from some updates in a new edition.
35 reviews
October 3, 2022
good intermediate python book great for wanting to write more pythonic
52 reviews
March 23, 2024
A really strong technical book for newbies to Python and long term developers. This personally has helped me write better Python code in my day-to-day.
Profile Image for Arun.
211 reviews66 followers
September 14, 2024
Took my time with this one and it was pretty good like any other Effective series I had read. Skipped Chap 7 but I know where to find if I ever needed its wisdom.
63 reviews
October 28, 2024
Skipped over quite a few important bits - viz. metaclasses and concurrency. But got what I came for, really to-the-point with great examples.
Displaying 1 - 30 of 31 reviews

Can't find what you're looking for?

Get help and learn more about the design.