The Writing Idiomatic Python book is finally here! Chock full of code samples, you'll learn the "Pythonic" way to accomplish common tasks. Each idiom comes with a detailed description, example code showing the "wrong" way to do it, and code for the idiomatic, "Pythonic" alternative.
You'll find Writing Idiomatic Python a valuable tool. If you don't, I'll refund you the full price of the book (which is still yours to keep). Why offer a money back guarantee on a book? I like having that extra level of comfort, and I'm guessing you do too. If you're not satisfied for any reason, just email me at jeff@jeffknupp.com within 30 days of your purchase.
Note The book is still in the "beta" stage, with corrections and new content added frequently. If you order now (at the lower, preorder price), you'll receive the 74 page PDF or ePub today and you'll get every update as soon as it's written, automatically
Writing Idiomatic Python comes in two versions: one for Python 2.7.3+ and one for Python 3.3+. Each has a number of idioms unique to their Python version.
enumerate function in loops instead of creating an “index” variableProgrammers coming from other languages are used to explicitly declaring a variable to track the index of a container in a loop. For example, in C++:
for (int i=0; i < container.size(); ++i)
{
// Do stuff
}
In Python, the enumerate built-in function handles this role.
my_container = ['Larry', 'Moe', 'Curly']
index = 0
for element in my_container:
print ('{} {}'.format(index, element))
index += 1
my_container = ['Larry', 'Moe', 'Curly']
for index, element in enumerate(my_container):
print ('{} {}'.format(index, element))
Every programming language has its own idioms. Programming language idioms are nothing more than the generally accepted way of writing a certain piece of code. In Python, for example, there are many ways to write code that loops over the elements of a list. You could write:
i = 0
while i < len(my_list):
# do stuff...
i += 1
This would work just fine. But it's not idiomatic (or "Pythonic"). The idiomatic version is:
for i in my_list:
Consistently writing idiomatic code has a number of important benefits:
This was always the toughest part. Previously, the only ways to learn idiomatic Python were to
There is! Writing Idiomatic Python contains the most common
and important Python idioms in a format that maximizes
identification and understanding. Each idiom is
presented as a recommendation to write some commonly used piece of
code (like the for loop above). It is followed by an
explanation of why the idiom is important. It also contains
two code samples: the "Harmful" way to write it and the
"Idiomatic" way.
If your financial situation doesn't afford you the opportunity to purchase the book, email me at jeff@jeffknupp.com and I'll happily send you a free copy. Just be sure to note in your email which version you'd like.
I'm always eager to hear feedback, both positive and negative. I encourage you to email me at jeff@jeffknupp.com. I do my best to respond to all email within 24 hours.
I'm Jeff Knupp, professional software developer and frequent blogger. Check out my blog or follow me on twitter.