[Tutor] Some first notes on the "Python Cookbook"
Scot W. Stevenson
scot@possum.in-berlin.de
Wed, 28 Aug 2002 09:55:57 +0200
Hi there,
Erik and Danny had asked about the "Python Cookbook". I'm only on page 79
in the third chapter (plus a few pages where I cheated and skipped ahead),
but I can offer the following notes so far:
- If you sit down and actually work thru every recipe, you are going to
learn a /lot/. Yes, the recipes in the first chapter alone have a big
"Hey, way cool!" factor - the RPN calculator I posted uses recipes 1.1
(Swapping values without using a temporary variable, by Hamish Lawson) and
1.6 (Dispatching using a dictionary, by Dick Wall) that I picked up here.
But part of this is also simply because the book touches on so many parts
of the language, you tend to notice where you are unsure. For example, I
realized in recipe 1.2 that I had just skimmed over the *args and **kwargs
parts of function definitions when I was first learning the language and
couldn't really follow the trick that Brent Burley was trying to teach me.
Oops - ten points from Gryffindor.
- The book is full of higher level Python programming concepts that I
haven't seen in any of the three other books on the language I have read
so far. This might in fact turn out to be the most valuable part of the
book on the long run. Examples so far include:
1. Alex Martelli's comparison of the "Look before you leap" (LBYL), "Easier
to ask forgiveness than permission" (EAFP), and "Homogenize different
cases" (HDC) idioms in recipe 5.3 on page 169. I don't know if this is
basic computer science stuff that everbody else learns in Algorithms 101,
but I found the discussion fascinating.
2. The discussion of "Decorate-Sort-Undecorate" (DSU) and related concepts
in chapter two (Tim Peters and others), which teaches you to force your
problem into a form that you can use the built-in tools to solve - in
other words, if you have a very good hammer, it is a good idea to
transform everything into nails before you start working.
3. Various discussion on efficiency. These include accessing dictionaries
with "for item in dic.keys()" [loops thru dictionary] vs. "dic.has_key()"
[which uses the hash table]; the speed of filter, map and reduce vs. list
comprehension; prelocating a list as a list of None instead of calling
append; local variables are the fastest kind to access.
A lot of these principles fit in somewhere in a grey zone between the pure
syntax as given in introductory books and the high-level Python philosophy
as set out in http://www.python.org/dev/culture.html - collecting them in
small text could be enormously helpful to slightly more advanced newbies
[hint =8)]. A good title might be "So what is this 'Pythonic' style I keep
reading about?"
- There is quite a difference in the background that is expected from the
reader from chapter to chapter. While Matthew Wood expects you to be able
to cope with concepts like "reentrancy" [still haven't figured that one
out] and "thread-safety" in chapter two, chapter three starts off with
Fred L. Drake explaining the very basic string commands such as the use of
single or double quotes and splicing.
- A lot of the recipes compare Medieval Python (1.5.2) fragments with their
Renaissance Python (2.2) equivalents, which provides much-needed examples
to the new forms such as "super" and generators. Working thru both forms
show you just how powerful a few lines of Renaissance Python can be, and
will have you wanting to do everything with list comprehensions, except
when you can map or filter thru builtin functions.
Again, I'm only on page 79, so don't blame me if you go out and buy the
book and it turns out that it really goes downhill from page 80 on. So
far, I would say that it was well worth my money, and would recommend it
to anybody who has passed the absolute beginner phase. Having a bunch of
peer-reviewed code that is extensively commented is second best only to
places like this list where you can ask all sorts of questions, and
O'Reilly did a good job of putting it all together.
Y, Scot
--
Scot W. Stevenson wrote me on Wednesday, 28. Aug 2002 in Zepernick, Germany
on his happy little Linux system that has been up for 1710 hours
and has a CPU that is falling asleep at a system load of 0.04.