Tag Archives: Python

My review of Python Cookbook, 3rd Edition by O’Reilly Digital Media

Product: Python Cookbook, 3rd Edition by O’Reilly Digital Media
Submitted at: O’Reilly

Fantastic Cookbook
by JimSchubert from Seattle WA on 7/27/2013

Helpful examples, Well-written, Concise, Easy to understand, Accurate

Best Uses:
Intermediate, Student, Expert, Novice

Describe Yourself:

I read Python Cookbook as part of the O’Reilly Bloggers program. I would highly recommend Python Cookbook to developers with any level of expertise, provided they understand the basic syntax and usage of Python 3.

I don’t use Python regularly. I’ve stayed away from Python for a while because of the parallel development of the 2.x and 3.x versions of Python. One of the things I loved about this book is that the authors clearly state that the intention of the book is to cover Python 3 only. In fact, many of the recipes won’t work in Python 2.x. The book’s contents have made me rethink avoiding Python.

I consider myself somewhat familiar with Python basics, which is required if you want to gain the most from this book. While the book does start of with some simple recipes (like list comprehensions, string manipulations, math operations), it eventually delves more into advanced topics like metaprogramming, socket programming, and the intricacies of working with threads.

It may not cover extremely advanced topics in Python, but the book comes close. The last chapter discusses interacting with C libraries which you may already have experience with if you’ve read Gray Hat Python (also available from O’Reilly). I consider language interop a pretty advanced topic.

In all, I was very pleased with the content and delivery of this book. I generally bookmark one or two pages in a book, but with this book I ended with 12 bookmarked pages. It definitely left me excited about developing applications with Python 3.

One caveat to readers: some of these recipes are for demonstration purposes only and should not be used in production code. For example, recipe 10.11 shows how you’d load modules from a remote machine using import hooks. Just because you *can* do this, doesn’t mean you should.

Python 3.0 handles Dictionary differently

I read through Python 3.0 from Developer’s Library about 2 months ago. I decided to skim through another introductory level book used at MIT so I could try keeping Python 2.5.x and Python 3.x separate.

While reading about Python 3.0, I didn’t catch this difference. Dictionary processing has changed a little bit in the new version of Python.

For instance:

Python 2.5.x (pg 112 from MIT's reading material):
>>> letterCounts = {}
>>> for letter in "Mississippi":
letterCounts[letter] = letterCounts.get (letter, 0) + 1
>>> letterCounts
{’M’: 1, ’s’: 4, ’p’: 2, ’i’: 4}
>>> letterItems = letterCounts.items()
>>> letterItems.sort()
>>> print letterItems
[(’M’, 1), (’i’, 4), (’p’, 2), (’s’, 4)]
Python 3.1.x:
>>> letterCounts = {}
>>> for letter in "Mississippi":
letterCounts[letter] = letterCounts.get (letter, 0) + 1
>>> letterCounts
{'i': 4, 'p': 2, 's': 4, 'M': 1}
>>> letterItems = list(letterCounts.items())
>>> letterItems.sort()
>>> letterItems
[('M', 1), ('i', 4), ('p', 2), ('s', 4)]

This is a slight difference in code. In fact, the Python 3 code will work in previous versions. But, if you tried the Python 2.5.x code in Python 3, you will notice that letterItems can not be sorted. This is because

letterItems = letterCounts.items()

returns type:

<class 'dict_items'>

An explanation of what is going on can be found here.

“Python 3.0 changes keys(), values() and items() so they return a lightweight set-like object, effectively making them behave like the olditer* methods, and removed the iter* methods themselves. In Python 3.0”