ShopSpell

Better Python Code: A Guide for Aspiring Experts [Paperback]

$54.99       ($100.00 Shipping)
72 available
  • Category: Books (Computers)
  • Author:  Mertz, David
  • Author:  Mertz, David
  • ISBN-10:  0138320942
  • ISBN-10:  0138320942
  • ISBN-13:  9780138320942
  • ISBN-13:  9780138320942
  • Publisher:  Addison-Wesley Professional
  • Publisher:  Addison-Wesley Professional
  • Pages:  288
  • Pages:  288
  • Binding:  Paperback
  • Binding:  Paperback
  • SKU:  0138320942-11-MPOD
  • SKU:  0138320942-11-MPOD
  • Item ID: 106968776
  • Seller: ShopSpell
  • Ships in: 2 business days
  • Transit time: Up to 4 business days
  • Delivery by: Sep 28 to Sep 30

Foreword xvii
Preface xix
Acknowledgments xxv
About the Author xxvii

Introduction 1

Chapter 1: Looping Over the Wrong Things 3
1.1 (Rarely) Generate a List for Iteration 3
1.2 Use enumerate() Instead of Looping Over an Index 6
1.3 Don't Iterate Over dict.keys() When You Want dict.items() 8
1.4 Mutating an Object During Iteration 9
1.5 for Loops Are More Idiomatic Than while Loops 12
1.6 The Walrus Operator for Loop-and-a-Half Blocks 13
1.7 zip() Simplifies Using Multiple Iterables 15
1.8 zip(strict=True) and itertools.zip_longest() 17
1.9 Wrapping Up 20

Chapter 2: Confusing Equality with Identity 21
2.1 Late Binding of Closures 21
2.2 Overchecking for Boolean Values 25
2.3 Comparing x == None 28
2.4 Misunderstanding Mutable Default Arguments 29
2.5 Copies versus References to Mutable Objects 33
2.6 Confusing is with == (in the Presence of Interning) 35
2.7 Wrapping Up 37

Chapter 3: A Grab Bag of Python Gotchas 39
3.1 Naming Things 39
3.2 Quadratic Behavior of Naive String Concatenation 52
3.3 Use a Context Manager to Open a File 56
3.4 Optional Argument key to .sort() and sorted() 59
3.5 Use dict.get() for Uncertain Keys 62
3.6 Wrapping Up 64

Chapter 4: Advanced Python Usage 67
4.1 Comparing type(x) == type(y) 67
4.2 Naming Things (Revisited) 71
4.3 Keep Less-Used Features in Mind 79
4.4 Type Annotations Are Not Runtime Types 98
4.5 Wrapping Up 105