- Copyright 2015
- Dimensions: 7" x 9-1/8"
- Pages: 256
- Edition: 1st
-
EPUB (Watermarked)
- ISBN-10: 0-13-403440-6
- ISBN-13: 978-0-13-403440-9
“Each item in Slatkin’s Effective Python teaches a self-contained lesson with its own source code. This makes the book random-access: Items are easy to browse and study in whatever order the reader needs. I will be recommending Effective Python to students as an admirably compact source of mainstream advice on a very broad range of topics for the intermediate Python programmer.”
—Brandon Rhodes, software engineer at Dropbox and chair of PyCon 2016-2017
It’s easy to start coding 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.
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 59 Python best practices, tips, and shortcuts, and explains them with realistic code examples.
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 learn the best way to accomplish key tasks, so you can write code that’s easier to understand, maintain, and improve.
Key features include
- Actionable guidelines for all major areas of Python 3.x and 2.x development, with detailed explanations and examples
- Best practices for writing functions that clarify intention, promote reuse, and avoid bugs
- Coverage of how to accurately express behaviors with classes and objects
- Guidance on how to avoid pitfalls with metaclasses and dynamic attributes
- More efficient approaches to concurrency and parallelism
- Better techniques and idioms for using Python’s built-in modules
- Tools and best practices for collaborative development
- Solutions for debugging, testing, and optimization in order to improve quality and performance
Table of Contents
-
Chapter 1: Pythonic Thinking
-
Item 1: Know Which Version of Python You’re Using
-
Item 2: Follow the PEP 8 Style Guide
-
Item 3: Know the Differences Between bytes, str, and unicode
-
Item 4: Write Helper Functions Instead of Complex Expressions
-
Item 5: Know How to Slice Sequences
-
Item 6: Avoid Using start, end, and stride in a Single Slice
-
Item 7: Use List Comprehensions Instead of map and filter
-
Item 8: Avoid More Than Two Expressions in List Comprehensions
-
Item 9: Consider Generator Expressions for Large Comprehensions
-
Item 10: Prefer enumerate Over range
-
Item 11: Use zip to Process Iterators in Parallel
-
Item 12: Avoid else Blocks After for and while Loops
-
Item 13: Take Advantage of Each Block in try/except/else/finally
-
-
Chapter 2: Functions
-
Item 14: Prefer Exceptions to Returning None
-
Item 15: Know How Closures Interact with Variable Scope
-
Item 16: Consider Generators Instead of Returning Lists
-
Item 17: Be Defensive When Iterating Over Arguments
-
Item 18: Reduce Visual Noise with Variable Positional Arguments
-
Item 19: Provide Optional Behavior with Keyword Arguments
-
Item 20: Use None and Docstrings to Specify Dynamic Default Arguments
-
Item 21: Enforce Clarity with Keyword-Only Arguments
-
-
Chapter 3: Classes and Inheritance
-
Item 22: Prefer Helper Classes Over Bookkeeping with Dictionaries and Tuples
-
Item 23: Accept Functions for Simple Interfaces Instead of Classes
-
Item 24: Use @classmethod Polymorphism to Construct Objects Generically
-
Item 25: Initialize Parent Classes with super
-
Item 26: Use Multiple Inheritance Only for Mix-in Utility Classes
-
Item 27: Prefer Public Attributes Over Private Ones
-
Item 28: Inherit from collections.abc for Custom Container Types
-
-
Chapter 4: Metaclasses and Attributes
-
Item 29: Use Plain Attributes Instead of Get and Set Methods
-
Item 30: Consider @property Instead of