Prix bas
CHF57.35
Pas encore paru. Cet article sera disponible le 27.11.2024
Auteur
Brett Slatkin is a Principal Software Engineer at Google in the Office of the CTO, focusing on emerging technologies. He co-founded Google Surveys, launched Google Clouds first product (App Engine), and co-created the PubSubHubbub protocolall using Python. Brett has been writing Python code professionally for the past 19 years and has made numerous contributions to open-source projects.
Texte du rabat
Master the art of Python programming with 125 actionable best practices to write more efficient, readable, and maintainable code. Python is a versatile and powerful language, but leveraging its full potential requires more than just knowing the syntax. Effective Python: 125 Specific Ways to Write Better Python, 3rd Edition is your comprehensive guide to mastering Python's unique strengths and avoiding its hidden pitfalls. This updated edition builds on the acclaimed second edition, expanding from 90 to 125 best practices that are essential for writing high-quality Python code. Drawing on years of experience at Google, Brett Slatkin offers clear, concise, and practical advice for both new and experienced Python developers. Each item in the book provides insight into the "Pythonic" way of programming, helping you understand how to write code that is not only effective but also elegant and maintainable. Whether you're building web applications, analyzing data, writing automation scripts, or training AI models, this book will equip you with the skills to make a significant impact using Python. Key Features of the 3rd Edition: Expanded Content: Now with 125 actionable guidelines, including 35 entirely new items. Updated Best Practices: Reflects the latest features in Python releases up to version 3.13. New Chapters: Additional chapters on how to build robust programs that achieve high performance. Advanced Topics: In-depth coverage of creating C-extension modules and interfacing with native shared libraries. Practical Examples: Realistic code examples that illustrate each best practice.
Contenu
Preface xvii
Acknowledgments xxiii
About the Author xxv
Chapter 1: Pythonic Thinking 1
Item 1: Know Which Version of Python Youre Using 1
Item 2: Follow the PEP 8 Style Guide 3
Item 3: Never Expect Python to Detect Errors at Compile Time 6
Item 4: Write Helper Functions Instead of Complex Expressions 8
Item 5: Prefer Multiple-Assignment Unpacking Over Indexing 11
Item 6: Always Surround Single-Element Tuples with Parentheses 16
Item 7: Consider Conditional Expressions for Simple Inline Logic 19
Item 8: Prevent Repetition with Assignment Expressions 24
Item 9: Consider match for Destructuring in Flow Control; Avoid When if Statements Are Sufficient 30
Chapter 2: Strings and Slicing 41
Item 10: Know the Differences Between bytes and str 41
Item 11: Prefer Interpolated F-Strings over C-Style Format Strings and str.format 47
Item 12: Understand the Difference Between repr and str when Printing Objects 58
Item 13: Prefer Explicit String Concatenation over Implicit, Especially in Lists 62
Item 14: Know How to Slice Sequences 67
Item 15: Avoid Striding and Slicing in a Single Expression 70
Item 16: Prefer Catch-All Unpacking Over Slicing 72
Chapter 3: Loops and Iterators 77
Item 17: Prefer enumerate over range 77
Item 18: Use zip to Process Iterators in Parallel 79
Item 19: Avoid else Blocks After for and while Loops 82
Item 20: Never Use for Loop Variables After the Loop Ends 85
Item 21: Be Defensive when Iterating over Arguments 87
Item 22: Never Modify Containers While Iterating over Them; Use Copies or Caches Instead 92
Item 23: Pass Iterators to any and all for Efficient Short-Circuiting Logic 98
Item 24: Consider itertools for Working with Iterators and Generators 102
Chapter 4: Dictionaries 109
Item 25: Be Cautious when Relying on Dictionary Insertion Ordering 109
Item 26: Prefer get over in and KeyError to Handle Missing Dictionary Keys 117
Item 27: Prefer defaultdict over setdefault to Handle Missing Items in Internal State 122
Item 28: Know How to Construct Key-Dependent Default Values with missing 124
Item 29: Compose Classes Instead of Deeply Nesting Dictionaries, Lists, and Tuples 127
Chapter 5: Functions 135
Item 30: Know That Function Arguments Can Be Mutated 135
Item 31: Return Dedicated Result Objects Instead of Requiring Function Callers to Unpack More Than Three Variables 138
Item 32: Prefer Raising Exceptions to Returning None 142
Item 33: Know How Closures Interact with Variable Scope and nonlocal 145
Item 34: Reduce Visual Noise with Variable Positional Arguments 150
Item 35: Provide Optional Behavior with Keyword Arguments 153
Item 36: Use None and Docstrings to Specify Dynamic Default Arguments 157
Item 37: Enforce Clarity with Keyword-Only and Positional-Only Arguments 161
Item 38: Define Function Decorators with functools.wraps 166
Item 39: Prefer functools.partial over lambda Expressions for Glue Functions 169
Chapter 6: Comprehensions and Generators 173
Item 40: Use Comprehensions Instead of map and filter 173
Item 41: Avoid More Than Two Control Subexpressions in Comprehensions 176
Item 42: Reduce Repetition in Comprehensions with Assignment Expressions 178
Item 43: Consider Generators Instead of Returning Lists 182
Item 44: Consider Generator Expressions for Large List Comprehensions 184
&nbs…