Notes

Author: 👤 Brett Slatkin

Type: #book

Genres: #comp-sci


Effective Python

90 Specific Ways to Write Better Python

1 Pythonic Thinking

Item 2: Follow the PEP 8 Style Guide

Item 3: Know the Differences Between bytes and str

Item 4: Prefer Interpolated F-Strings over C-style Format Strings

Item 5: Write Helper Functions Instead of Complex Expressions

Item 6: Prefer Multiple Assignment Unpacking Over Indexing

Item 7: Prefer enumerate Over range

Item 8 Use zip to Process Iterators in Parallel

Item 10: Prevent Repetition with Assignment Expressions

2 Lists and Dictionaries

Item 11: Know How to Slice Sequences

Item 12: Avoid Striding and Slicing in a Single Expression

Item 13: Prefer Catch-All Unpacking Over Slicing

Item 14: Sort by Complex Criteria Using the key Parameter

Item 15: Be Cautious When Relying on dict Insertion Ordering

Item 16: Prefer get Over in and KeyError to Handle Missing Dictionary Keys

Item 17: Prefer defaultdict Over setdefault to Handle Missing Items in Internal State

Item 18: Know How to Construct Key-Dependent Default Values with __missing__

3 Functions

Item 19: Never Unpack More Than Three Variables When Functions Return Multiple Values

Item 20: Prefer Raising Exceptions to Returning None

Item 21: Know How Closures Interact with Variable Scope

Item 22: Reduce Visual Noise with Variable Positional Arguments

Item 23: Provide Optional Behaviour with Keyword Arguments

Item 24: Use None and Docstrings to Specify Dynamic Default Arguments

Item 25: Keyword-Only and Positional-Only Arguments

Item 26: Define Function Decorators with functools.wraps

4 Comprehensions and Generators

Item 27: Use Comprehensions Instead of map and filter

Item 28: Avoid More Than Two Control Sub-expressions in Comprehensions

Item 29: Avoid Repeated Work in Comprehensions by Using Assignment Expressions

Item 30: Consider Generators Instead of Returning Lists

Item 31: Be Defensive When Iterating Over Arguments

Item 32: Consider Generator Expressions for Large List Comprehensions

Item 33: Compose Multiple Generators with yield from

Item 36: Consider itertools for Working with Iterators and Generators

5 Classes and Interfaces

Item 37: Compose Classes Instead of Nesting Many Levels of Built-in Types

Item 38: Accept Functions Instead of Classes for Simple Interfaces

Item 40: Initialise Parent Classes with super

Item 42: Prefer Public Attributes Over Private Ones

Item 43: Inherit from collections.abc for Custom Container Types

6 Metaclasses and Attributes

Item 44: Use Plain Attributes Instead of Setter and Getter Methods

8 Robustness and Performance

Item 65: Take Advantage of Each Block in try/except/else/finally

Item 66: Consider contextlib and with Statements for Reusable try/finally Behaviour

Item 67: Use datetime Instead of time for Local Clocks

Item 68: Make pickle Reliable with copyreg

Item 69: Use decimal When Precision is Paramount

Item 70: Profile Before Optimising

Item 71: Prefer deque for Producer-Consumer Queues

Item 72: Consider Searching Sorted Sequences with bisect

Item 73: Know How to Use heapq for Priority Queues

Item 83: Use Virtual Environments for Isolated and Reproducible Dependencies

Item 84: Write Docstrings for Every Function, Class, and Module

Item 85: Use Packages to Organise Modules and Provide Stable APIs

Item 87: Define a Root Exception to Insulate Callers from APIs

Item 88: Know How to Break Circular Dependencies

Item 89: Consider warnings to Refactor and Migrate Usage

Item 90: Consider Static Analysis via typing to Obviate Bugs