"Learning the ins and outs of Python is difficult--and with this book you'll be able to focus on the practical skills that really matter. Discover the 'hidden gold' in Python’s standard library and start writing clean and Pythonic code today. With Python Tricks: The Book you’ll discover Python’s best practices and the power of beautiful & Pythonic code with simple examples and a step-by-step narrative."--Provided by publisher “I don’t even feel like I’ve scratched the surface of what I can do with Python” With Python Tricks: The Book you’ll discover Python’s best practices and the power of beautiful & Pythonic code with simple examples and a step-by-step narrative. You'll get one step closer to mastering Python, so you can write beautiful and idiomatic code that comes to you naturally. Learning the ins and outs of Python is difficult—and with this book you'll be able to focus on the practical skills that really matter. Discover the “hidden gold” in Python’s standard library and start writing clean and Pythonic code today. Who Should Read This Book: If you’re wondering which lesser known parts in Python you should know about , you’ll get a roadmap with this book. Discover cool (yet practical!) Python tricks and blow your coworkers’ minds in your next code review. If you’ve got experience with legacy versions of Python , the book will get you up to speed with modern patterns and features introduced in Python 3 and backported to Python 2. If you’ve worked with other programming languages and you want to get up to speed with Python , you’ll pick up the idioms and practical tips you need to become a confident and effective Pythonista. If you want to make Python your own and learn how to write clean and Pythonic code , you’ll discover best practices and little-known tricks to round out your knowledge. What Python Developers Say About The Book: "I kept thinking that I wished I had access to a book like this when I started learning Python many years ago." — Mariatta Wijaya, Python Core Developer "This book makes you write better Python code!" — Bob Belderbos, Software Developer at Oracle "Far from being just a shallow collection of snippets, this book will leave the attentive reader with a deeper understanding of the inner workings of Python as well as an appreciation for its beauty." — Ben Felder, Pythonista "It's like having a seasoned tutor explaining, well, tricks!" — Daniel Meyer, Sr. Desktop Administrator at Tesla Inc. Contents 7 Foreword 10 Introduction 12 What's a Python Trick? 12 What This Book Will Do for You 14 How to Read This Book 15 Patterns for Cleaner Python 16 Covering Your A** With Assertions 17 Complacent Comma Placement 26 Context Managers and the with Statement 30 Underscores, Dunders, and More 37 A Shocking Truth About String Formatting 49 The Zen of Python Easter Egg 57 Effective Functions 58 Python's Functions Are First-Class 59 Lambdas Are Single-Expression Functions 69 The Power of Decorators 74 Fun With *args and **kwargs 87 Function Argument Unpacking 92 Nothing to Return Here 95 Classes & OOP 98 Object Comparisons: is vs == 99 String Conversion (Every Class Needs a __repr__) 102 Defining Your Own Exception Classes 112 Cloning Objects for Fun and Profit 117 Abstract Base Classes Keep Inheritance in Check 125 What Namedtuples Are Good For 129 Class vs Instance Variable Pitfalls 137 Instance, Class, and Static Methods Demystified 144 Common Data Structures in Python 154 Dictionaries, Maps, and Hashtables 157 Array Data Structures 164 Records, Structs, and Data Transfer Objects 174 Sets and Multisets 186 Stacks (LIFOs) 190 Queues (FIFOs) 196 Priority Queues 202 Looping & Iteration 206 Writing Pythonic Loops 207 Comprehending Comprehensions 211 List Slicing Tricks and the Sushi Operator 215 Beautiful Iterators 219 Generators Are Simplified Iterators 232 Generator Expressions 240 Iterator Chains 247 Dictionary Tricks 251 Dictionary Default Values 252 Sorting Dictionaries for Fun and Profit 256 Emulating Switch/Case Statements With Dicts 260 The Craziest Dict Expression in the West 265 So Many Ways to Merge Dictionaries 272 Dictionary Pretty-Printing 275 Pythonic Productivity Techniques 278 Exploring Python Modules and Objects 279 Isolating Project Dependencies With Virtualenv 283 Peeking Behind the Bytecode Curtain 289 Closing Thoughts 294 Free Weekly Tips for Python Developers 296 PythonistaCafe: A Community for Python Developers 297