Dan Bader
What Pythonistas Say About Python Tricks: The Book
”I love love love the book. It’s like having a seasoned tutor explaining,
well, tricks! I’m learning Python on the job and I’m coming from powershell,
which I learned on the job—so lots of new, great stuff. Whenever
I get stuck in Python (usually with flask blueprints or I feel like my
code could be more Pythonic) I post questions in our internal Python chat room.
I’m often amazed at some of the answers coworkers give me. Dict comprehensions,
lambdas, and generators often pepper their feedback. I
am always impressed and yet flabbergasted at how powerful Python
is when you know these tricks and can implement them correctly.
Your book was exactly what I wanted to help get me from a bewildered
powershell scripter to someone who knows how and when to use these
Pythonic ‘tricks’ everyone has been talking about.
As someone who doesn’t have my degree in CS it’s nice to have the text
to explain things that others might have learned when they were classically
educated. I am really enjoying the book and am subscribed to
the emails as well, which is how I found out about the book.”
— Daniel Meyer, Sr. Desktop Administrator at Tesla Inc.
”I first heard about your book from a co-worker who wanted to
trick me with your example of how dictionaries are built. I was
almost 100% sure about the reason why the end product was a much
smaller/simpler dictionary but I must confess that
I did not expect the outcome :)
He showed me the book via video conferencing and I sort of skimmed
through it as he flipped the pages for me, and I was immediately curious to read more.
That same afternoon I purchased my own copy and proceeded to read
your explanation for the way dictionaries are created in Python and
later that day, as I met a different co-worker for coffee,
I used the same trick on him :)
He then sprung a different question on the same principle, and because
of the way you explained things in your book, I was able tonot*
guess the result but correctly answer what the outcome would be. That
means that you did a great job at explaining things :)*
I am not new in Python and some of the concepts in some of the chapters
are not new to me, but I must say that I do get something out of
every chapter so far, so kudos for writing a very nice book and for doing
a fantastic job at explaining concepts behind the tricks! I’m very
much looking forward to the updates and I will certainly let my friends
and co-workers know about your book.”
— Og Maciel, Python Developer at Red Hat
”I really enjoyed reading Dan’s book. He explains important Python
aspects with clear examples (using two twin cats to explain ‘is‘ vs ‘==‘
for example).
It is not just code samples, it discusses relevant implementation details
comprehensibly. What really matters though is that this book makes
you write better Python code!
The book is actually responsible for recent new good Python habits I
picked up, for example: using custom exceptions and ABC’s (I found
Dan’s blog searching for abstract classes.) These new learnings alone
are worth the price.”
— Bob Belderbos, Engineer at Oracle & Co-Founder of PyBites
Book Details
Price
|
3.00 |
---|---|
Pages
| 299 p |
File Size
|
1,276 KB |
File Type
|
PDF format |
ISBN
| 9781775093305 (paperback) 9781775093312 (electronic) |
Copyright©
| Dan Bader (dbader.org), 2016–2017 |
It’s been almost ten years since I first got acquainted with Python as a
programming language. When I first learned Python many years ago,
it was with a little reluctance. I had been programming in a different
language before, and all of the sudden at work, I was assigned to a
different team where everyone used Python. That was the beginning
of my own Python journey.
When I was first introduced to Python, I was told that it was going to
be easy, that I should be able to pick it up quickly. When I asked my
colleagues for resources for learning Python, all they gave me was a
link to Python’s official documentation. Reading the documentation
was confusing at first, and it really took me a while before I even felt
comfortable navigating through it. Often I found myself needing to
look for answers in StackOverflow.
Coming from a different programming language, I wasn’t looking for
just any resource for learning how to program or what classes and
objects are. I was looking for specific resources that would teach me
the features of Python, what sets it apart, and how writing in Python
is different than writing code in another language.
It really has taken me many years to fully appreciate this language. As
I read Dan’s book, I kept thinking that I wished I had access to a book
like this when I started learning Python many years ago.
For example, one of the many unique Python features that surprised
me at first were list comprehensions. As Dan mentions in the book,
a tell of someone who just came to Python from a different language
is the way they use for-loops. I recall one of the earliest code review
comments I got when I started programming in Python was, “Why
not use list comprehension here?” Dan explains this concept clearly
in section 6, starting by showing how to loop the Pythonic way and
building it all the way up to iterators and generators.
In chapter 2.5, Dan discusses the different ways to do string formatting
in Python. String formatting is one of those things that defy the
Zen of Python, that there should only be one obvious way to do things.
Dan shows us the different ways, including my favorite new addition
to the language, the f-strings, and he also explains the pros and cons
of each method.
The Pythonic Productivity Techniques section is another great resource.
It covers aspects beyond the Python programming language,
and also includes tips on how to debug your programs, how to manage
the dependencies, and gives you a peek inside Python bytecode.
It truly is an honor and my pleasure to introduce this book, Python
Tricks, by my friend, Dan Bader.
By contributing to Python as a CPython core developer, I get connected
to many members of the community. In my journey, I found
mentors, allies, and made many new friends. They remind me that
Python is not just about the code, Python is a community.
Mastering Python programming isn’t just about grasping the theoretical
aspects of the language. It’s just as much about understanding and
adopting the conventions and best practices used by its community.
Dan’s book will help you on this journey. I’m convinced that you’ll be
more confident when writing Python programs after reading it.
— Mariatta Wijaya, Python Core Developer (mariatta.ca)
Table of Contents
Contents 6
Foreword 9
1 Introduction 11
1.1 What’s a Python Trick? . . . . . . . . . . . . . . . . 11
1.2 What This Book Will Do for You . . . . . . . . . . . 13
1.3 How to Read This Book . . . . . . . . . . . . . . . . 14
2 Patterns for Cleaner Python 15
2.1 Covering Your A** With Assertions . . . . . . . . . . 16
2.2 Complacent Comma Placement . . . . . . . . . . . . 25
2.3 Context Managers and the with Statement . . . . . . 29
2.4 Underscores, Dunders, and More . . . . . . . . . . . 36
2.5 A Shocking Truth About String Formatting . . . . . . 48
2.6 “The Zen of Python” Easter Egg . . . . . . . . . . . . 56
3 Effective Functions 57
3.1 Python’s Functions Are First-Class . . . . . . . . . . 58
3.2 Lambdas Are Single-Expression Functions . . . . . . 68
3.3 The Power of Decorators . . . . . . . . . . . . . . . 73
3.4 Fun With *args and **kwargs . . . . . . . . . . . . 86
3.5 Function Argument Unpacking . . . . . . . . . . . . 91
3.6 Nothing to Return Here . . . . . . . . . . . . . . . . 94
4 Classes & OOP 97
4.1 Object Comparisons: “is” vs “==” . . . . . . . . . . . 98
4.2 String Conversion (Every Class Needs a __repr__) . 101
4.3 Defining Your Own Exception Classes . . . . . . . . 111
4.4 Cloning Objects for Fun and Profit . . . . . . . . . . 116
4.5 Abstract Base Classes Keep Inheritance in Check . . . 124
4.6 What Namedtuples Are Good For . . . . . . . . . . . 128
4.7 Class vs Instance Variable Pitfalls . . . . . . . . . . . 136
4.8 Instance, Class, and Static Methods Demystified . . . 143
5 Common Data Structures in Python 153
5.1 Dictionaries, Maps, and Hashtables . . . . . . . . . 156
5.2 Array Data Structures . . . . . . . . . . . . . . . . . 163
5.3 Records, Structs, and Data Transfer Objects . . . . . 173
5.4 Sets and Multisets . . . . . . . . . . . . . . . . . . . 185
5.5 Stacks (LIFOs) . . . . . . . . . . . . . . . . . . . . 189
5.6 Queues (FIFOs) . . . . . . . . . . . . . . . . . . . . 195
5.7 Priority Queues . . . . . . . . . . . . . . . . . . . . 201
6 Looping & Iteration 205
6.1 Writing Pythonic Loops . . . . . . . . . . . . . . . . 206
6.2 Comprehending Comprehensions . . . . . . . . . . 210
6.3 List Slicing Tricks and the Sushi Operator . . . . . . 214
6.4 Beautiful Iterators . . . . . . . . . . . . . . . . . . . 218
6.5 Generators Are Simplified Iterators . . . . . . . . . 231
6.6 Generator Expressions . . . . . . . . . . . . . . . . 239
6.7 Iterator Chains . . . . . . . . . . . . . . . . . . . . 246
7 Dictionary Tricks 250
7.1 Dictionary Default Values . . . . . . . . . . . . . . . 251
7.2 Sorting Dictionaries for Fun and Profit . . . . . . . . 255
7.3 Emulating Switch/Case Statements With Dicts . . . . 259
7.4 The Craziest Dict Expression in the West . . . . . . . 264
7.5 So Many Ways to Merge Dictionaries . . . . . . . . . 271
7.6 Dictionary Pretty-Printing . . . . . . . . . . . . . . 274
8 Pythonic Productivity Techniques 277
8.1 Exploring Python Modules and Objects . . . . . . . . 278
8.2 Isolating Project Dependencies With Virtualenv . . . 282
8.3 Peeking Behind the Bytecode Curtain . . . . . . . . . 288
9 Closing Thoughts 293
9.1 Free Weekly Tips for Python Developers . . . . . . . 295
9.2 PythonistaCafe: A Community for Python Developers 296
What’s a Python Trick?
Python Trick: A short Python code snippet meant as a
teaching tool. A Python Trick either teaches an aspect of
Python with a simple illustration, or it serves as a motivating
example, enabling you to dig deeper and develop an intuitive understanding.