The Perl Journal February 2003
Practical Python is a complete course in Python programming, from the elements of the language to some rather advanced example projects. It's a fine work, from the writing to the pacing to the layout and other production values, including technical review. It's undoubtedly one of the fastest and most painless ways to get Python under your belt.
You're probably familiar with Python, but in case you're not: Python is an objectified scripting language similar to Perl and occupying a similar niche, but possessing a more rigid and streamlined structure than Perl. Python's popularity is growing among those who find dealing with the immensity of Perl and its praxis a bit like trying to snow-swim your way out of an avalanche.
Author Magnus Hetland is the founder of the AnyGui project, which allows Pythoners to slap a GUI onto their code. He is a powerful Python programmer and a good teacher. His example projects are more interesting and useful than such projects usually are in books of this sort.
Hetland is big on Python, but he's not a language warrior. Aside from a few sly digs at C++, which frankly was never Python's main competition anyway, Hetland sticks to business. Practical Python is pretty much all pedagogy with little panegyric.
A neat web blurb on the book, along with the Table of Contents and a sample chapter, is at http://www.apress.com/book/ bookDisplay.html?bID=93.
The copyright page ofPractical Pythonnotes a site for code download, but has the interesting caveat that the downloader "will have to answer a few questions about the book" in order to download, which I've never seen Apress do before. Perhaps they read their web logs and discovered they were giving away a few thousand copies of source code for each book they sell?
I recently chatted with Magnus via ICQ from his home in Norway.
TPJ: Please tell me about workwhat you are doing these days, what projects are you working on?
MH: I'm a Ph.D. student at the Norwegian University of Science and Technology. Hopefully, I'll finish my thesis this spring. My main area of research at the moment is knowledge discovery in time series and sequences in general.
TPJ: What does that mean?
MH: It basically means finding interesting patterns and predicting trends in sequences of various kinds; for example, predicting stock prices and the likealthough I haven't had too much luck in that particular area.
TPJ: Does Python help you in this? Or is Python something separate?
MH: WellI use C++ for the really heavy stuff, such as genetic programming with costly computations and the like, but I use Python for almost everything else. I find it particularly useful for preparing data that I've gathered from various sources. File processing, and especially text-file processing, is much easier in Python than in C++ in my opinion. I guess, basically, I use Python where I find it feasible. I do use Python for some number crunching, too, with extensions such as numarray or Pyrex.
TPJ: Parsing and string handling in general was never easy in C/C++. Most people use Perl. What's the case for the Python?
MH: I used Perl quite a bit, too, earlier on. And I liked it quite a bit more than C at the time. But when I discovered Python, I found that it had the same advantages when compared to C, but the syntax and the elegance of the object model appealed to me.
TPJ: I'm sure our readers understand the syntax advantage. What's the "elegance" of the object model of Python?
MH: I guess in the days when I switched from Perl to Python, the difference for me was mainly that I couldn't completely grok the Perl model at all, while the Python model seemed quite obvious. What the status of Perl in this area is at the moment, I don't really know, so I'm not in a position to compare the two. But since version 2.2, Python has simplified its type and class systems, and I think the whole thing is now quite sleekalthough much of the sleekness is "under the hood."
TPJ: The IBM language ObjectRexx is very aesthetically satisfying from a pure object point of view. You use the term "Mixin class" in the book. MIXIN is an ObjectRexx keyword. Tell me about this.
MH: Right. I've seen it used in C++ too, I think. It's usable in any language with multiple inheritance. Basically, you write simple classes that implement only a limited piece of functionalitya single method, for example. Then you can use that as one of several parent classes, and inherit the limited functionality in addition to that of the "main" parent. Not really a formal conceptmore of a conventional way of structuring code. For example, in the Python Standard Library, you have a socket server class. In addition, you have other classes such as a threading mix-in that can be used to get a threaded socket server.
The similarity of [Python to] Rexx has not escaped Python programmers. There is a comparison of the two languages at http://www.scoug.com/os24u/2001/pyREXXex.html and another at http://www.python.org/tim_one/000330.html. Tim Peters, who makes the latter, is a top Python guru.
TPJ: Do Python users experience all the problems that Perl users do when the language undergoes a revision?
MH: WellI don't really know what problems Perl users face, but of course there are problems. Any changes that are not backwards compatible are problematic. Thankfully, the policy of Guido van Rossum and the Python team on this point is to try to avoid backwards incompatibilities as much as possible. Also, there is a system of gradual change, with warnings being issued by the interpreter when you use features that are obsolete.
TPJ: How did you get started writing the book?
MH: Actually, I was contacted by W. Jason Gilmore from Apress, who had read my online tutorials "Instant Hacking" and "Instant Python." He liked them a lot, and wanted me to write a book in somewhat the same spirit. Even though the book is, of course, a lot longer than the tutorials, I have tried to keep it just as light and humorous.
TPJ: How did your English get so good? While I read the book, I thought "either his English is very good or he has a very good editor."
MH: Actually, I went to an English preschool, so I knew some English already at the age of six. I suppose that has given me a head start. I also had very good editors at Apress, of course.
TPJ: English preschool? Is that a common mode of education in Norway?
MH: No, not really. It was just one yearmy parents thought it was a good idea. But we do have quite a bit of English in our school system, so I suppose Norwegians know more English than people in many other countries. Writing the book was a great learning experienceI learned about intricacies and subtleties both in the English language and in Python.
TPJ: What are some of the "intricacies and subtleties" you discovered in writing the book?
MH: Well, for one thing, I learned about standard traps like using "since" instead of "because," which may be misinterpreted. Similarly, I learned about such things as restrictive and nonrestrictive clauses and the uses of "which" and "that." As for Python, I learned so many things that I can hardly remember any of them. But most of what I learned is documented in the book. Much of what I learned can be attributed to my diligent and competent editors.
TPJ: I guess my perspective on Python is foreshortened because by reading your book, I more than doubled my knowledge of the language! How long have you been doing Python?
MH: From around 1997, I think. And, yes, there is quite a lot of knowledge packed into the book. Even though it starts out as a book for newbies, there is quite a lot to be learned. I've included lots of useful little things that many people might not know, and traps that I've seen many people fall into (through comp.lang.python).
TPJ: What are people doing with it? What is stimulating the growth? I mean, with Perl there's CPAN; why go play in the Python sandbox?
MH: Well, there are the typical case histories for a language at http://www.python-in-business.org/success/. But there is actually a repository somewhat like CPAN underway for Python, too (see http://www.python.org/peps/pep-0243.html and http://www .python.org/peps/pep-0301.html), that will be linked with the Python distribution system, Distutils. As for why one would want to use Pythonit's hard to give any hard and fast reasons. I think that many features of the language (especially its syntax) make it easy to learn, and its modularity and focus on object-orientedness makes it easy to build large systems without the hassle of languages such as C and C++.
There is some empirical evidence for productivity increase with high-level languages such as LISP, Perl, Python, or Ruby. But choosing between them may be more of an irrational thing, I suppose. There are many arguments for most languages. I guess the best thing to do if one is curious about Python is to try it. There is virtually no cost in setting up an interpreter, and playing around with it can give you some notion of how the language works. My tutorial Instant Python (http://hetland.org/python/instant-python/) is meant as an introduction to people who already know other languages. It should give you an overview of the language in, say, half an hour.
TPJ: For some reason, your publisher, Apress, puts notes about URLs for source code on the copyright page where almost no one finds and reads them. And they're not very informative. This is just a small glitch from a publisher that is in other respects a very good publisher. But I don't see any URL in the book for corrections/errata (I've found minor typos here and there).
MH: There should be a link from the Apress page, and there is also a link from my page at ppython.com.
TPJ: Wasn't writing Practical Python quite a distraction as you work towards your Ph.D.?
MH: Yes, it was. My advisor wasn't all too happy with it. I hadn't planned on spending as much time on it as I ended up doing. But it seems that I'll be able to finish my thesis on time anyway (hopefully), and then the book is only a bonus. It certainly has given me a lot of training that may be useful when writing scientific articles and the like. I've tried my very best to convey the sense of fun I feel when programming in Python. I suppose that's been my main goalto show that programming can be fun and that anyone can do it; to some degree, anyway.
If you're a programmer, you ought to love programming. I feel sorry for those entering the business only in the hope of earning loads of money. I noticed a sharp upturn in the number of students taking computer-related courses at our university at the end of the '90s. Many of them had little interest or aptitude for programming. Of course, you don't have to know programming very well to work with computers, but I think you should. It's basic craft.
TPJ