Python is one of the most widely used programming languages in the world. It is used everywhere from primary school education to workaday web development, to the most advanced scientific research institutes of the world. However, like all programming languages, Python has a collection of "Pythonic" ways of accomplishing tasks that are easy to overlook, especially when habits are borrowed wholesale from work in other programming languages.
Better Python Code is a guide to Pythonic programming. The book presents common mistakes that Python developers make--even Python developers who have used the language for years--often because Python sometimes presents "attractive nuisances." Throughout, the book is a guide to better programming in the core Python language.
Each section shows a concrete but concise example of some misunderstanding or bad habit in action. Each section contains a description of what is wrong with the sample code and a suggestion for one or more better ways to code equivalent functionality without the initial pitfall. Every pitfall addressed in this book reflects foibles, errors, and misunderstandings that the author as seen in concrete, widely used code bases written by experienced developers, over his 25 years of writing Python.
Both beginners and developers with decades of experience will learn to correct limitations in the code they write after reflecting on these discussions.
Author(s): David Mertz
Publisher: Addison-Wesley Professional
Year: 2023
Language: English
Pages: 240
Cover Page
Title Page
Contents
Table of Contents
Foreword
Preface
About The Book
Obtaining The Tools Used In This Book
Other Useful Tools
Acknowledgments
About the Author
Introduction
Chapter 1. Looping Over the Wrong Things
1.1. (Rarely) Generate A List For Iteration
1.2. Use enumerate() Instead Of Looping Over An Index
1.3. Don’t Iterate Over dict.keys() When You Want dict.items()
1.4. Mutating An Object During Iteration
1.5. for Loops Are More Idiomatic Than while Loops
1.6. The Walrus Operator For “Loop-and-a-Half” Blocks
1.7. zip() Simplifies Using Multiple Iterables
1.8. zip(strict=True) And itertools.zip_longest()
1.9. Wrapping Up
Chapter 2. Confusing Equality with Identity
2.1. Late Binding Of Closures
2.2. Overchecking For Boolean Values
2.3. Comparing x == None
2.4. Misunderstanding Mutable Default Arguments
2.5. Copies Versus References To Mutable Objects
2.6. Confusing is With == (In The Presence Of Interning)
2.7. Wrapping Up
Chapter 3. A Grab Bag of Python Gotchas
3.1. Naming Things
3.2. Quadratic Behavior Of Naive String Concatenation
3.3. Use A Context Manager To Open A File
3.4. Optional Argument key To .sort() And sorted()
3.5. Use dict.get() For Uncertain Keys
3.6. Wrapping Up
Chapter 4. Advanced Python Usage
4.1. Comparing type(x) == type(y)
4.2. Naming Things (Revisited)
4.3. Keep Less-Used Features In Mind
4.4. Type Annotations Are Not Runtime Types
4.5. Wrapping Up
Chapter 5. Just Because You Can, It Doesn't Mean You Should…
5.1. Metaclasses
5.2. Monkeypatching
5.3. Getters And Setters
5.4. It’s Easier To Ask For Forgiveness Than Permission
5.5. Structural Pattern Matching
5.6. Regular Expressions And Catastrophic Backtracking
5.7. Wrapping Up
Chapter 6. Picking the Right Data Structure
6.1. collections.defaultdict
6.2. collections.Counter
6.3. collections.deque
6.4. collections.ChainMap
6.5. Dataclasses And Namedtuples
6.6. Efficient Concrete Sequences
6.7. Wrapping Up
Chapter 7. Misusing Data Structures
7.1. Quadratic Behavior Of Repeated List Search
7.2. Deleting Or Adding Elements To The Middle Of A List
7.3. Strings Are Iterables Of Strings
7.4. (Often) Use enum Rather Than CONSTANT
7.5. Learn Less Common Dictionary Methods
7.6. JSON Does Not Round-Trip Cleanly To Python
7.7. Rolling Your Own Data Structures
7.8. Wrapping Up
Chapter 8. Security
8.1. Kinds Of Randomness
8.2. Putting Passwords Or Other Secrets In "Secure" Source Code
8.3. "Rolling Your Own" Security Mechanisms
8.4. Use SSL/TLS For Microservices
8.5. Using The Third-Party requests Library
8.6. SQL Injection Attacks When Not Using DB-API
8.7. Don’t Use assert To Check Safety Assumptions
8.8. Wrapping Up
Chapter 9. Numeric Computation in Python
9.1. Understanding IEEE-754 Floating Point Numbers
9.2. Numeric Datatypes
9.3. Wrapping Up
Appendix: Topics for Other Books
Test Driven Development
Concurrency
Packaging
Type Checking
Numeric And Data Frame Libraries