Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror
×
Books Media Programming Book Reviews Technology

Dive Into Python 309

AccordionGuy writes "If you've ever spent an afternoon in the "Computers" section of a bookstore going through the programming language books, you've probably noticed that most of them seem to exist only to boost a publishing company's fortunes by capitalizing on the hot new programming language of the moment. These books -- essentially glorified bookends -- seem to follow the same format, cover the same subjects and aside from the tiny flourishes that are part of each author's particular writing style, are indistinguishable from each other. Reading them, one gets the feeling that its primary purpose is to allow the author to make some payments on a car or mortgage. I have a few of these books and they're gathering dust on the bookshelf farthest away from my desk." For deVilla's review of Dive Into Python, a book that inhabits a completely different category, read on below.
Dive Into Python
author Mark Pilgrim
pages 432
publisher Apress
rating 9
reviewer Joey deVilla
ISBN 1590593561
summary The "desert island" Python book

However, from time to time, you can find a programming language book that stands apart. You can tell from the way the author writes, the topics s/he covers, the unique presentation style and insight that s/he brings that the book is a labor of love. These books enjoy placement on the shelf closest to my desk -- that is, if they're not propped open beside my computer. Dive Into Python is such a book.

One thing that sets Dive Into Python apart from many other programming language books is that its author, Mark Pilgrim, didn't originally plan to make any money from it. As we often say in Open Source circles, he simply had an itch and decided to scratch it. Mark explains this in a story on his weblog in the form of a dialog between him and his manager after showing him a rough 20-page draft:

Manager: "This is really good. You could probably make some money off this someday."
Mark: "Maybe, but I'm not going to. I'm giving it away for free."
Manager: "Why would you do that?"
Mark: "Because this is the way I want the world to work."
Manager: "But the world doesn't work that way."
Mark: "Mine does."

First released in late October 2000 and published in online and downloadable forms under the GNU Free Documentation License, Dive Into Python had grown in fits and starts until 2003, when Mark declared the project closed. Even as an unfinished work, it was held in such high regard by the Python community that developers consistently recommended it; it was also included with ActiveState's Python and FreeBSD's ports distributions. When Mark announced that Apress had decided to pay him to finish the book and publish it, it became the most-anticipated book on Python ever. Even better, Apress has been gracious enough to allow Mark's world to work way it always has: Dive Into Python is still available for free download and is still under the GNU FDL.

What's in Dive Into Python

Many programming language books follow what I like to call the "Computer Science 101 Format", with the first few chapters devoted to covering basic concepts that any moderately experienced programmer already knows. Whenever I leaf through such a book and encounter a chapter that tries to reintroduce me to data types, looping or branching, I feel cheated; I'm essentially paying for a big chunk of book that I'll never read. If you've ever been annoyed by such filler, you'll find Dive Into Python a refreshing change. Rather than wasting time and trees devoting whole chapters to rehashing Computer Science 101, Mark chose to build each chapter after the first around a program that illustrates a number of Python features and programming techniques.

The programs upon which Dive Into Python's chapters are based strike a carefully-maintained balance. They are rich enough to illustrate a number of points and be the basis for some "real world" code, yet small enough to be comprehensible tutorials. For example, chapters 2 and 3 are based on "Your First Python Program", which is a mere six lines of code. However, in those six lines, you are introduced to function declarations, documentation strings, objects and their attributes, importing modules, Python's indentation rules, the "if __name__" idiom, dictionaries, lists, tuples, string formatting and list comprehensions. Within the first hundred pages, a point where many books are re-acquainting you with the "else" keyword, Dive Into Python covers the aforementioned topics as well as Python's reflection capabilities, list filtering, the "and-or trick", lambda functions, OOP and exception handling, all with enough thoroughness to be useful. After reading Dive Into Python, you may have trouble reading other programming language books because they'll seem glacially slow and fluff-laden in comparison.

For the first two-thirds of the book, Mark continues with this approach, presenting a program and then analyzing it to see what makes it tick, teaching Python and oftentimes a programming technique along the way. Each program covers useful tasks that you're likely to run into while programming and does so in an interesting way. At the same time, concepts are introduced in a way that makes sense. For instance, chapter 4 covers two topics that mesh together quite well -- exceptions and file handling -- and it does this by exploring an interesting application: a program that displays the ID3 tag information about each file in your MP3 collection. Later chapters explore regular expressions, HTML and XML processing and Web services. By the time you've finished the first two-thirds of Dive Into Python, you'll have been introduced to enough Python to start writing a wide array of "real world" applications. The book might have benefited from having a chapter covering database access, a task that's at least as common or as useful as accessing Web services, but that's a minor complaint.

While the first two-thirds of the book concerns itself with helping the reader become a Python programmer, the final third is about elevating Python programmers above mere competence. It covers useful topics (albeit rarely-covered in language books) such as refactoring and performance optimization as well as ones that may be new to even some experienced programmers: unit testing, functional programming and dynamic functions. Each chapter in this section is still based on an example program, but rather than analyzing a completed program, its evolution is traced. Although you can get by as a Python programmer without ever reading the material in this section, you'll be a much better one for having done so.

In keeping with the spirit of Python, Mark writes the chapters to present the material as completely and clearly as possible without extra clutter. If there's any additional material that doesn't apply directly to what he's trying to explain, he provides references or links to that material rather than attempting to "fatten up" the book.

The book's long gestation period, assisted by years of reader feedback and James Cox's editing has paid off. It doesn't have the rushed feel that many language-of-the-moment books have (especially the ones written by an army of authors, each one taking a chapter). As far as I know, there isn't any of the sloppiness that pervades many programming books these days, save one instance of the popular typo "teh" (and really, what truly 1337 book doesn't have one of these?).

Mark is aware that Python is likely not to be the reader's first programming language; it's more likely to be some descendant of ALGOL (or more precisely, a language that borrows heavily from either C or BASIC). He also knows that many programmers tend to misapply techniques from the languages with which they're familiar to the language they're learning. With these in mind, he's taken great care to introduce Python idioms as soon as possible. If you follow his advice, you'll be writing "real" Python and taking advantage of what the language has to offer rather than just writing Python-flavored version of whatever programming language you're most comfortable with.

Dive Into Python's Audience

The "user level" specified on the back cover of this book says "Beginner - Intermediate", which I feel is a little misleading. As I mentioned earlier, the book takes great care not to rehash topics with which programmers with some experience are already familiar and is written with the assumption that the reader is proficient in at least one object-oriented programming language. I think many programming novices would be overwhelmed with the speed with which Python features are introduced.

Experienced programmers, whether they are new to Python or are fluent with the language will benefit the most from the book. One programmer I know works with Python daily and and even submitted a patch to wxPython; even he said that Dive Into Python showed him things about Python that he never knew. If you're tired of books aimed at "Introduction to Computer Science" students, you're going to love this book. This doesn't mean that people who don't normally program can't benefit from the book: Joi Ito, who is a tech entrepreneur and not a programmer, learned enough from Dive Into Python to put together jibot, a bot for the IRC channel that bears his name. If you're new to programming, you might want to make Dive Into Python your second book or supplement it with an introductory text such as Apress' own Practical Python, O'Reilly's Learning Python or the free online book How To Think Like a Computer Scientist (the Python edition).

Conclusion

Dive Into Python may be one of the thinnest programming language books on my shelf, but it's also one of the best. Whether you're an experienced programmer looking to get into Python or grizzled Python veteran who remembers the days when you had to import the string module, Dive Into Python is your "desert island" Python book. If you're new to programming but have heard all the wonderful things about Python, make sure that this is the second programming book you read. My congratulations to Mark Pilgrim on an excellent book and authorial debut!

(Remember, you don't have to just listen to my effusive praise. Dive Into Python is available for free at diveintopython.org. Read it for yourself and if you like it, vote with your dollar!)


You can purchase Dive Into Python from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.

This discussion has been archived. No new comments can be posted.

Dive Into Python

Comments Filter:
  • My 2 cents (Score:5, Insightful)

    by KillaKen187 ( 794540 ) on Wednesday September 08, 2004 @03:19PM (#10193153) Homepage
    Python is great very robust, easy to use, and capable to do a lot of things quickly. I also know that Mark is a great guy. I have sent him emails on trouble I have had on things and he has given great responses to them. Very upstanding guy. All blessings to Mark Pilgrim and his contribution to OSS with Python.
  • by BuzzLY ( 267639 ) on Wednesday September 08, 2004 @03:23PM (#10193212) Homepage
    As an author of one of those "bookends," I'd have to assume that AccordianGuy has never written one. The money is certainly not a huge draw -- it's not enough to live on, for sure. I can't speak for all authors, but for me, it was an opportunity to do two things that I love - play with one of my favorite programming languages, and write. Perhaps it really is crap, but at least I enjoyed writing it!
  • Python (Score:4, Insightful)

    by jefu ( 53450 ) on Wednesday September 08, 2004 @04:07PM (#10193800) Homepage Journal
    A few years back I wrote some scripts for web input processing. I knew a smattering of Perl so wrote them in Perl. They worked ok as far as I could tell . I had other things to do so stopped working on them, then went back after a while to add some functionality and discovered that I had no idea what I had done or how. Eventually I worked it out and added the functionality I wanted (and comments, yup) and went away again. The next time I went back I had to learn the whole thing again and my comments and cleaned up code didn't help.

    So I'd heard about Python and that it was good, so (since I like the process of learning new languages) I decided to try rewriting the scripts in python. In about two days I had them doing everything the perl had done and the added functionality as well and with remarkably few bugs.

    Eventually I went back to add on more functionality and "Lo!" I had no trouble reading my Python code and even better adding in the new stuff was simple.

    The biggest problem with Python has been the lack of a good book, I'll be considering "Dive into Python" carefully - being in the education biz I'm looking for a really good Python book for students.

  • by reallocate ( 142797 ) on Wednesday September 08, 2004 @04:23PM (#10193995)
    >>... one gets the feeling that its primary purpose is to allow the author to make some payments on a car or mortgage...

    Geez, how did some people become so naive? Just figuring out that professional authors write for money? And you're offended by it?

  • by Black Perl ( 12686 ) on Wednesday September 08, 2004 @05:39PM (#10194944)
    Just wanted to add one more for reference:

    With Python: (8 seconds)
    [ x ** 2 for x in xrange(100000) if x % 2 == 0 ]
    With Ruby: (2 seconds)
    (1..100_000).select { |x| x % 2 == 0 }.map { |x| x ** 2 }
    With Perl: (<= 1 second)
    map { $_ ** 2 } grep { $_ % 2 == 0 } (1..100000);
  • by Anonymous Coward on Wednesday September 08, 2004 @05:54PM (#10195125)
    (I'll leave aside your contradiction in the very first two statements, that Peter refuses to suggest only "one" implementation, and in the next you say that he uses Allegro only).

    Actually, instead of being stupid like you suggest, the free implementations are moving towards broad compatibility with each other instead.

    It is dumb to insist there only be one Lisp implementation because there are different trade-offs to be made in terms of CPU, memory, convenience, etc. The point of the ANSI standard is to make this possible.

    Thankfully, people with more vision than your limited Python-mindset are working and making the open source Lisp compilers very strong.

    Python barely counts as a programming language, in my eyes. If no one cares to provide a proper compiler for a language, then the language doesn't matter!

    P.S. Peter uses Allegro in his book because he will be distributing a copy of Allegro 7.0 Personal edition on CD with the book; courtesy of Franz Inc. However, all of his advice should pretty much carry over to any other Lisp implementation. So tell me, Free Software zealot, does it matter which implementation you use WHEN THERE IS A STANDARD? Go back and cry to Guido, your Python-mommy, if you can't handle that amount of freedom. The rest of us prefer having choice.
  • by Anonymous Coward on Wednesday September 08, 2004 @06:09PM (#10195261)
    I don't know if any Common Lisp implementation wants to be the de-facto open source lisp, but SBCL [sbcl.org] is very popular in the News, Web, and IRC. Throw Emacs and SLIME [common-lisp.net] into the mix and you get a really nice system.
  • by Anonymous Coward on Wednesday September 08, 2004 @07:02PM (#10195670)
    "For too many people, the "Why-to" is "because I heard you can make lots of money as a programmer". To answer this demand, colleges and universities churn out rank and file incompetent and ambivalent programmers, weilding their Microsoft inspired toolchest of Visual Basic 6 or MS Access, they flood the resume pool, and pollute the code base with their lazy half-hearted attempts at development, causing project failures and frustration on a massive scale and dramtically increasing the costs of corporate IT."

    Gee I will never understand why people ruin an otherwise good post with a political agenda.

    Here's a couple things for you.

    1) Project failure, frustrations and cost overuns predate the VB and MS Access era. But it is nice to have a scapegoat, instead of looking at the real reasons.

    2) There's nothing wrong with asking for money for your work. Disregard those whom are envious of you, and wish to bring you down by implying your a bad person for asking to get paid for your work (you'll hear things like "do it for the love").
  • by Anonymous Coward on Wednesday September 08, 2004 @07:40PM (#10195975)
    There is another very nifty way to instantly optimize python: psyco

    http://psyco.sourceforge.net/index.html

    all you need is:
    import psyco
    psyco.full()

    to get a more than 30% or so speed up:
    without Psyco:
    N = 100000
    With List Comps it took: 0.346352 seconds
    N = 1000000
    With List Comps it took: 3.468986 seconds

    With psyco:
    N = 100000
    With List Comps it took: 0.197049 seconds
    N = 1000000
    With List Comps it took: 2.186394 seconds

  • Ok, I'll bite... (Score:3, Insightful)

    by Big Sean O ( 317186 ) on Wednesday September 08, 2004 @10:16PM (#10197133)
    It's probably flamebait, but here goes...

    Instead of working for the man, Mark did what he wanted to do. Now the 'FLOSS-zealot' has written a well-respected book and the manager looks like a selfish loser.

    I'd rather be a well-respected author than a manager with no imagination.

    Seems like Mark decided to be the change he wanted in the world. Good for him.

Top Ten Things Overheard At The ANSI C Draft Committee Meetings: (5) All right, who's the wiseguy who stuck this trigraph stuff in here?

Working...