Effective Python: 59 Specific Ways to Write Better Python

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Author(s): Brett Slatkin
Series: Effective Software Development Series
Publisher: Addison-Wesley Professional
Year: 2015

Language: English
Commentary: Deleted code images and "Click here to view code image" texts.
Pages: 225
Tags: python

Code Snippets......Page 0
Title Page......Page 2
Copyright Page......Page 3
Praise for Effective Python......Page 5
Dedication Page......Page 7
Contents......Page 8
What This Book Covers......Page 11
Conventions Used in This Book......Page 12
Where to Get the Code and Errata......Page 13
Acknowledgments......Page 14
About the Author......Page 15
Item 1: Know Which Version of Python You’re Using......Page 16
Item 2: Follow the PEP 8 Style Guide......Page 17
Item 3: Know the Differences Between bytes, str, and unicode......Page 19
Item 4: Write Helper Functions Instead of Complex Expressions......Page 22
Item 5: Know How to Slice Sequences......Page 24
Item 6: Avoid Using start, end, and stride in a Single Slice......Page 26
Item 7: Use List Comprehensions Instead of map and filter......Page 28
Item 8: Avoid More Than Two Expressions in List Comprehensions......Page 29
Item 9: Consider Generator Expressions for Large Comprehensions......Page 31
Item 10: Prefer enumerate Over range......Page 32
Item 11: Use zip to Process Iterators in Parallel......Page 34
Item 12: Avoid else Blocks After for and while Loops......Page 35
Item 13: Take Advantage of Each Block in try/except/else/finally......Page 38
Item 14: Prefer Exceptions to Returning None......Page 40
Item 15: Know How Closures Interact with Variable Scope......Page 42
Item 16: Consider Generators Instead of Returning Lists......Page 45
Item 17: Be Defensive When Iterating Over Arguments......Page 47
Item 18: Reduce Visual Noise with Variable Positional Arguments......Page 51
Item 19: Provide Optional Behavior with Keyword Arguments......Page 53
Item 20: Use None and Docstrings to Specify Dynamic Default Arguments......Page 56
Item 21: Enforce Clarity with Keyword-Only Arguments......Page 58
Item 22: Prefer Helper Classes Over Bookkeeping with Dictionaries and Tuples......Page 62
Item 23: Accept Functions for Simple Interfaces Instead of Classes......Page 67
Item 24: Use @classmethod Polymorphism to Construct Objects Generically......Page 70
Item 25: Initialize Parent Classes with super......Page 74
Item 26: Use Multiple Inheritance Only for Mix-in Utility Classes......Page 78
Item 27: Prefer Public Attributes Over Private Ones......Page 81
Item 28: Inherit from collections.abc for Custom Container Types......Page 86
Item 29: Use Plain Attributes Instead of Get and Set Methods......Page 90
Item 30: Consider @property Instead of Refactoring Attributes......Page 94
Item 31: Use Descriptors for Reusable @property Methods......Page 97
Item 32: Use __getattr__, __getattribute__, and __setattr__ for Lazy Attributes......Page 101
Item 33: Validate Subclasses with Metaclasses......Page 106
Item 34: Register Class Existence with Metaclasses......Page 108
Item 35: Annotate Class Attributes with Metaclasses......Page 112
Item 36: Use subprocess to Manage Child Processes......Page 115
Item 37: Use Threads for Blocking I/O, Avoid for Parallelism......Page 119
Item 38: Use Lock to Prevent Data Races in Threads......Page 122
Item 39: Use Queue to Coordinate Work Between Threads......Page 125
Item 40: Consider Coroutines to Run Many Functions Concurrently......Page 131
Item 41: Consider concurrent.futures for True Parallelism......Page 139
Item 42: Define Function Decorators with functools.wraps......Page 143
Item 43: Consider contextlib and with Statements for Reusable try/finally Behavior......Page 145
Item 44: Make pickle Reliable with copyreg......Page 148
Item 45: Use datetime Instead of time for Local Clocks......Page 153
Item 46: Use Built-in Algorithms and Data Structures......Page 157
Item 47: Use decimal When Precision Is Paramount......Page 161
Item 48: Know Where to Find Community-Built Modules......Page 163
Item 49: Write Docstrings for Every Function, Class, and Module......Page 165
Item 50: Use Packages to Organize Modules and Provide Stable APIs......Page 169
Item 51: Define a Root Exception to Insulate Callers from APIs......Page 173
Item 52: Know How to Break Circular Dependencies......Page 175
Item 53: Use Virtual Environments for Isolated and Reproducible Dependencies......Page 180
Item 54: Consider Module-Scoped Code to Configure Deployment Environments......Page 185
Item 55: Use repr Strings for Debugging Output......Page 187
Item 56: Test Everything with unittest......Page 189
Item 57: Consider Interactive Debugging with pdb......Page 192
Item 58: Profile Before Optimizing......Page 194
Item 59: Use tracemalloc to Understand Memory Usage and Leaks......Page 198
Index......Page 201