Slashdot Mirror


Dive Into Python

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.

28 of 309 comments (clear)

  1. Don't forget the author's website by Anonymous Coward · · Score: 5, Informative

    Dive Into Mark. Lots of interesting/useful stuff.

  2. Broken Link by keiferb · · Score: 5, Informative

    Here's a good link to diveintopython.org. The one at the end of the review is horked.

  3. Awesome by izakage · · Score: 1, Informative

    This book was awesome, I've been using it for awhile, and it's a really good python guide.

  4. Python: The Movie by PissingInTheWind · · Score: 5, Informative

    For all of you who still haven't seen it, I strongly recommend this 'propaganda' gem from the Python community:

    Video: Introducing Python

    Features GvR, ESR, etc.

    It's so bad it hurts. You'll want to show it to all your friends.

    --

    A message from the system administrator: 'I've upped my priority. Now up yours.'
  5. python's list processing rules by graveyhead · · Score: 4, Informative
    For those who haven't tinkered yet, or are just getting started with Python, I've found one of it's greatest strengths is it's ability to munge lists.

    e.g, look at this super-terse, but still legible, sub-array code:
    l = l[1:]
    List comprehensions are another great feature:
    l = [ do_something(x) for x in l ]
    This is so amazingly compact when compared with list processing in Java or C++.

    Anyhow, hats off to Guido Van Rossum for such a great language. I'll have to check this book out... I've been using the python cookbook (OReilly) and the documentation on the site almost exclusively...
    --
    std::disclaimer<std::legalese> sig=new std::disclaimer; sig->dump(); delete sig;
    1. Re:python's list processing rules by Mendenhall · · Score: 4, Informative

      Actually, that took me a while to figure out, too. If you try it with 50000 instead of 100000, on my machine python takes 0.384 seconds while ruby takes 0.537 seconds. With 100000, the comparison is much as you show (except about twice as fast in both cases). If, instead of squaring x, you take x alone, python is also faster.

      Apparently, there is something quite different about ruby's handling of long integers (>32) bits from python's. In the python case, this expression automatically switches to infinite-precision integers (at least for python 2.3). I don't use ruby, so I don't know what it is producing when the numbers get beyond 32 bits. Does it automatically go to long integers?

      Anyway, the speed difference in this pair of examples seems contrived to get ruby to do something much faster than python, since it requires 'special' values including integer overflow to exercise this effect. Maybe ruby really has better long integer handling than python. Can any on who knows ruby comment on the result of the overflow?

    2. Re:python's list processing rules by Anonymous Coward · · Score: 1, Informative

      I don't use ruby, so I don't know what it is producing when the numbers get beyond 32 bits. Does it automatically go to long integers?

      Yes:

      puts 10.class # => Fixnum

      puts 1000000000000.class # => Bignum

      No idea which is faster in general, though. Ruby seems slower more often, but who cares, if I want speed I just write a C library and load it from Ruby (Ruby has a cool feature that lets you load arbitrary C libraries on the fly... I'm sure Python has something similar but I can program so much faster in Ruby).

    3. Re:python's list processing rules by LittleDan · · Score: 2, Informative

      There are good reasons why Python is great, but this isn't one of them. Scheme can do this much more tersely. for the first example,

      l = l[1:]

      This could be done in HTDP's Scheme dialect as

      (rest l)

      (that didn't do the same thing, but in good Schemely style, you probably wouldn't be updating a variable for something like that). The Python version would be O(n), while the Scheme version would be O(1). This is because Python uses arrays and annoys the rest of the programmer world calling them lists. Scheme, OTOH, uses linked lists, where the rest operation doesn't require copying most of the structure like Python does.

      For the second example, the list comprehension, the Python version

      l = [do_something(x) for x in l]

      is again longer than the scheme version (which I'm leaving out assignment for):

      (map do-something l)

      Don't like the parens? You might want to check out Haskell, which is similar to Scheme in many respects but has significant indentation and list comprehensions. Python is very un-unique in its ability to do these things you've listed.

      Daniel Ehrenberg
  6. It's also in Debian. by jonathan_atkinson · · Score: 4, Informative
    If you use Debian, you can just:
    # su
    # apt-get install diveintopython
    And the book will be downloaded and placed into /usr/share/doc/diveintopython/

    --Jon
    --
    Cleanstick.org: Dumb weblog about nothing
  7. Re:a series of well planned coincidences. by bhsurfer · · Score: 5, Informative

    Have you read The Practice of Programming by Brian Kernighan & Rob Pike? I thought it was a pretty good "why-to" sort of book that didn't spoon feed you anything, plus it gives examples/assignments in multiple languages so you can see the differences between them. If you haven't read this one, check it out. They probably have it at the library if you don't want to read it, but I'm glad I own a copy.

    --
    Those are my principles, and if you don't like them... well, I have others.
    Groucho Marx
  8. Re:Thoughts of Python... by nkh · · Score: 3, Informative

    Indentation to create blocks is stupid when you begin to learn Python. And after a day or two you don't think about it anymore. The language is cool, and tabulation is just a detail when you've got OOP, regexpes...

  9. Re:Thoughts of Python... by Anonymous Coward · · Score: 4, Informative
    "Wow, sounds interesting but when you cut and paste chunks of don't you run the risk of shooting a hole in your foot and then somehow choking to death on the fountain of blood?"

    Get a smart text editor; one that can handle this. When I cut'n'paste Java code in Eclipse, for example, it makes all of thew whitespace perfect. Indenting and outdenting code should be no more than a few keystrokes. ("<<" or ">>" in vim's visual mode. ctrl-[ or ctrl-] in Eclipse. etc.)

    This is something I always did in other languages anyway, so Python's whitespace handling never bothered me. Improper whitespace drives me nuts (too hard to read), so a language that enforces proper whitespace is perfect to me.

  10. mirror by Kedder · · Score: 4, Informative

    here is a mirror

  11. Also in the ActivePython docs by jalefkowit · · Score: 4, Informative

    If you use ActiveState's free distribution of Python for Windows, ActivePython, the electronic version of Dive into Python is included in the documentation file under "Helpful Resources". Very handy!

  12. Re:Thoughts of Python... by tjkslashdot · · Score: 2, Informative

    All I need to do now is find a shop that actually uses Python...


    Google, ILM, and NASA all use Python. See the Python Quotes page.
  13. Re:Thoughts of Python... by Anonymous Coward · · Score: 3, Informative

    Emacs python-mode makes adjusting indent levels for blocks trivial:

    Paste or select a block, C-> indents, C- outdents

    Don't bother selecting your whole file & re-indenting the whole thing.

    Note that with a brace/delimiter oriented language, you often have to manually insert/delete delimiters...

    C-M-\ (indent-region) still works to adjust indent levels (i.e., if somebody indented blocks w/ 2 spaces and your setup is 4 spaces), but it won't move code between blocks.

    Entering new code in python-mode is also easy - it knows when to begin a block, to end a block you just backspace once-per-desired-outdent level.

    TAB still indents a line to where emacs thinks it should go, backspace jumps back an indent level.

    But the real power of python-mode is in executing code blocks in an interactive interpreter:
    C-c C-c to execute the buffer
    C-M-x to execute a function/class definition
    C-| to execute the selected region

    Nice. :-)

    kb

  14. Completely Slashdotted!! by chris_sawtell · · Score: 2, Informative

    So here's the contents page from Google's cache:-
    Dive Into Python Table Of Contents

  15. Re:Python Compiler by cryptochrome · · Score: 2, Informative

    there's py2exe... but that's just an executable with the python interpreter and all necessary modules and code included, IIRC.

    --

    ---If you can't trust a nerd, who can you trust?

  16. Re:Thoughts of Python... by Anonymous Coward · · Score: 1, Informative
    My conceptual problem is that when I cut and paste a block of code to a new place, there is no acceptible default indent level - I might want to end the previous block and start a new one, or I might want to stick this under the if, or maybe it should go outside it, and there's no way for the editor to automatically know and do the right thing.

    A good algorithm is just to put the first line at the current indent level and follow the indent/outdent patterns from there. So you position the cursor correctly and then it goes from there. If you make a mistake, you just hit the indent/outdent button after pasting.

    I often in emacs select the whole file and tell it to correct the identation... which is impossible in python?

    Which is unnecessary in Python. The indentation is always correct, and that's that.

  17. Re:What for almost absolute beginner? by Anonymous Coward · · Score: 1, Informative

    practical python by magnus lie hetland. good stuff. written by a norwegian dude

  18. With so many links in one article... by Just6979 · · Score: 2, Informative

    why didn't the author think to utilize NYU's Coral content distribution network (covered here on /. just a couple weeks ago) to keep the bandwidth off diveintopython.org? Now it's too late, since Coral can't access the site to get it in the cache.

    --
    --Justin
  19. Unslashdotted link to the book by Anonymous Coward · · Score: 2, Informative
  20. /.'ed but mirrored (sort of) by starkaudio · · Score: 3, Informative

    Drooling to give it a read, but can't get to the site? The previous link was to a version from 2002. If you go to google and search for anything along the lines of "diveintopython_5.4.orig.tar.gz" you'll find the May 20th, 2004 version is on many linux distro sites. Enjoy!

  21. Re:Thoughts of Python... by grumbel · · Score: 2, Informative

    ### Right, and even worse, its inability to encapsulate class-local methods (no data hiding => no OO!).

    I don't think that data hiding is all that important for OO, after all it doesn't add any functionality, it just restricts access a little bit and is in languages like C++ even easily workarounded via raw pointers or even more evil '#define private public' hacks. Worse of all, data hiding can make it actually impossible or very ugly to workaround bugs in a library.

    Anyway, as far as I know Python does data hiding via '__' prefixes infront of the variable name, should be good enough to tell anybody 'don't touch this' while still allowing access if absolutly necesarry.

  22. Python Hosting by Anonymous Coward · · Score: 1, Informative

    For those looking for some decent Python hosting (or PHP 5 even for that matter), I use http://www.a2webhosting.com/. They seem to know what they're doing.

  23. Re:'a mere 6 lines'? by Mr+Europe · · Score: 2, Informative

    And it includes all the documentation !

    No, really, writing a lot of code in very few characters or lines is not the target, it is not good practice, it only makes code fuzzy.

  24. Re:What for almost absolute beginner? by bindlestiff · · Score: 2, Informative

    As a small and simple tutorial guide I've enjoyed this book.

    Python: Visual QuickStart Guide
    ISBN:0201748843
    Peachpit Press

    Most of the examples are for the command line. This will encourage you to experiment and play. Creative play is where good programmers come from. It's not going to teach you how to write a big app or become a graphics wizard. It's certainly not suitable as your only reference book. It will give you an excellent introduction to the language in simple prose. It's small, lightweight, inexpensive and brief. It'll get you started, and you'll occasionally go back to it when you want a simple understandable explanation of a concept. I've bought some other tomes that weigh 10 times as much, cost 3 times and came with a CD, that aren't as useful. I like it, maybe it'll help you. I suspect it would be ideal for anyone who has trouble with english.

  25. Re:Thoughts of Python... by Ian+Bicking · · Score: 2, Informative
    Double-underscore should only be used if you are somehow worried about name collisions with subclasses, or you are otherwise a control freak. A single underscore is the proper way of indicating something is a private variable. There's nothing stopping you from accessing such a variable, but Python does not assume Python programmers are idiots, so it lets you choose. Except in the case of double underscore, which is why many Python programmers find them obnoxious (myself included).

    There are a variety of other data-hiding techniques. Enforced encapsulation isn't really an essential aspect of OO anyway; Smalltalk has no private methods (except by convention, like Python), and Smalltalk is quintessentially OO. Smalltalk does make all instance variables private; I'd probably agree that it would have been better if Python instance variables were private, but you can use property if you later decide to make a public attribute calculated, or one of the other cases where public attributes cause problems.