Python Cookbook (O’Reilly Cookbooks)
“. . . an excellent reference, though it . . . offers no tutorial for beginners.”
When I first offered to review The Python Cookbook there was hesitation, “A python cookbook?” No, it’s not a python cookbook.
Python is a computer programming language that takes its name from the 1970s British sketch comedy TV series Monty Python’s Flying Circus. Python falls into the category of computer languages that work well for creating small complex applications quickly.
What makes Python a good programming language is that fewer housekeeping statements have to be added before a program can run. What makes Python a poor programming language is the same lack of housekeeping that makes it easy to write code also leads to hazards that are difficult to catch.
The advantage that Python shares with more popular languages such as Java and C++ is the large number of open source programs available for use. A programmer may not have to write all the features of an application from scratch but build on or stitch together open source programs to do what they want.
The disadvantage of having so much already written code to choose from is separating the professional wheat from the amateurish chaff; there can be significant effort in finding something useful that doesn’t require a significant amount of modification to get working.
Over the past 30 years this reviewer has written applications in many different programming languages, from high-level to assembly, and has come to believe that code should be subservient to design: that having experience in architecture, structure, and algorithm is of greater value than having experience in any particular language.
Languages tend to come and go, while a good design if not timeless at least aids in the transition from design to code, making the transition regular if not mechanical. That a design should be as simple as possible—but no simpler (to borrow a quote) is difficult enough—the programming language chosen should not add to the difficulty.
Programmers will recognize “There Ain’t No Such Thing As A Free Lunch” (TANSTAAFL) in all of this. The downside of favoring design over code leads to not being an expert in any language, and that using constructs common across languages, always needing a language reference nearby, and intentionally avoiding advanced features—what Python programmers call “Pythonic”—has its drawbacks. The consequence of programming in a language-neutral style often results in less compact code—though one hopes more easily maintained and less likely to suffer from language evolution.
On to the review. The Python Cookbook is a detailed, lengthy reference covering a wide range of features in a “cookbook” style with programming “recipes.” Problems are posed and solutions provided as short programs, which are followed by discussion.
The Python Cookbook offers chapters on:
- Data structures
- Strings and text
- Numbers, Dates, and Time
- Iterators and Generators
- Data encoders and Processors
- Functions
- Classes and Objects
- Metaprogramming
- Modules and Packages
- Networking and Web programming
- Concurrency
- Utility scripts and System Administration
- Testing, Debugging and Exceptions
- C extensions
This reviewer’s experience with Python had been up to this point solely with Python version 2. With that in mind, I sampled recipes from the Python Cookbook for version 3, executed recipes under version 3.3, and compared the results to programs running under Python version 2.7.3 in side-by-side X-term windows on a MacBook Pro. As The Python Cookbook provides a large number of recipes, it was impractical to test each and every one; so I poked around, picking random problems and recipes from chapter to chapter.
The first difference between the two versions showed up on page three: the recipe for unpacking data records. Python 3 adds features for Lisp-like list processing that are not available in version 2 (Note to editor, Lisp is a programming language, not a speech impediment sometimes exhibited by six year olds on losing their baby teeth). Another difference detected was the “print” statement’s change in syntax. Version 3’s “print” is now a function; that is, print’s parameters have to be wrapped by parenthesis rather than following after the keyword separated by commas. This should not be a big deal except as print is a common statement, updating a program with many print statements could take some effort.
Accessing positional arguments in lists by use of the star “*” modifier also is new to version 3—though the example provided almost worked for version 2.7, the “*” was accepted but the function behaved incorrectly assuming an integer when the list data was floating-point.
Considering the seemingly random differences exhibited between version 2 and 3, and as The Python Cookbook itself made no special mention of differences between versions, I turned to Google to gain perspective on what was going on (and I suggest the reader who also is using version 2 do the same). One could imagine a programmer’s frustration from using a version 3 reference while debugging programs written for or executing under previous versions of the Python language.
Several chapters stand out and may interest Python programmers: metaprogramming shows how to create code wrappers that do not incur extra processing from wrapping one layer of code with another; there are recipes for lambda functions, the ins-and outs of source code analysis, python byte code disassembly, modules, packages and namespaces, how to print hex codes, iterate in reverse, process XML, create a Remote Procedure Call (RPC) server, and techniques for memory and time optimization.
The Python Cookbook also provides coding oddities such as bypassing __init__() by use of setattr(), which may leave the reader wondering why one would ever want to do this except perhaps to win a code obfuscation contest (Note: Yes, there are programming contests to see who can create the most unreadable code).
The Python Cookbook is an excellent reference, though it is recommended for the more experienced programmers for it showcases a mix of exceptional and arcane features, and while providing coding examples for difficult and uncommon problems it disappointingly offers no tutorial for beginners.