Book Review: Learning Python, 3rd Edition (1)

Utterly bewitched by this dramatic and thrilling panel from an xkcd.com comic strip, I have decided that it is time I learned the Python programming language.

Flying with Python

I come to Python as a bioinformatics scientist who is experienced in C, C++, PHP, Perl, and R. For the past two years, I have used Perl for 95% of the software I have written. But there are days when I grow weary of trying to write object-oriented Perl code in a clear and maintainable way. Perl 5 just isn’t a language designed to support object-oriented programming in a natural way. Hence, I thought I would try Python.

This post is Part 1 of what I plan to be a series of posts about the book Learning Python, 3rd Edition, which was written by Mark Lutz and published in October, 2007, by O’Reilly. This part of my review covers “Part I: Getting Started”, the first 62 pages of the book

In the preface, Mr. Lutz states that Learning Python is intended to be an introduction to the Python programming language for programmers and that it deliberately covers only the core Python language without providing guidance on application programming. Mr. Lutz encourages readers to continue on to Programming Python, 3rd Edition (of which he is also the author).

The preface also contains a three-page section, “Preparing for Python 3.0,” which contains a brain-numbing list of features and changes anticipated in Python 3.0. I believe it would have been more appropriate to place this material in an appendix.

Learning Python is organized into eight major parts:

  • Getting Started
  • Types and Operations
  • Statements and Syntax
  • Functions
  • Modules
  • Classes and OOP (Object Oriented Programming)
  • Exceptions and Tools
  • Appendixes

The book provides a deliberately bottom-up introduction to Python. This means the reader has to be patient, because Mr. Lutz teaches about each piece of the Python language without showing right away how to use the pieces together to write scripts. Consequently, I have found that I need to jump ahead in the book because I want to begin scripting right away.

Each chapter contains a short quiz, with the answers provided with each quiz. Each part of the book ends with a set of exercises, for which the solutions are provided in Appendix B.

In Chapter 1, Mr. Lutz discusses the strengths and weaknesses of Python. The strengths are many and significant:

  1. Python is designed to produce readable code, which makes the code easy to maintain and reuse.
  2. Python is designed up front to support object-oriented programming.
  3. Python code is more succinct than C++ or Java code.
  4. Python is portable and runs on Linux/Unix, Mac OS X, and Windows.
  5. Python comes with a large standard library.
  6. Python works well with other languages.
  7. Python is free.

The only weakness that Mr. Lutz acknowledges is that Python sometimes doesn’t run as quickly as compiled C and C++ code. A weakness mentioned by many others is that, contrary to other C-like programming languages (C, C++, Java, C#, Perl, and PHP), white space in Python code has syntactic significance. Many programmers object to this, and it does seem to generate problems with parsing the code.

Chapter 1 devotes a lot of space to discussing the philosphy behind Python.

In philosophy, Python adopts a somewhat minimalist approach. This means that although there are usually multiple ways to accomplish a coding task, there is usually just one obvious way…. In the Python way of thinking, explicit is better than implicit, and simple is better than complex.

This discussion provides a sharp contrast to Perl, for which the motto is, “There is more than one way to do it” (TIMTOWTDI). Perl contains many language constructs that make commands implicit rather than explicit, and this can make Perl code difficult to understand.

Mr. Lutz devotes nearly two pages (pp. 16–17) to a sidebar titled “Python is Engineering, Not Art,” in which the author discusses the relative merits of Python and Perl.

The short story is this: you can do everything in Python that you can in Perl, but you can read your code after you do it…. [author’s italics]

The somewhat longer story reflects the backgrounds of the designers of the two languages…. Python’s creator [Guido van Rossum] is a mathematician by training; as such, he produced a language with a high degree of uniformity—its syntax and toolset are remarkably coherent….

By contrast, the creator of the Perl language [Larry Wall] is a linguist, and its design reflects this heritage. There are many ways to accomplish the same tasks in Perl, and language constructs interact in context-sensitive and sometimes quite subtle ways—much like natural language….

But as anyone who has done any substantial code maintenance should be able to attest, freedom of expression is great for art, but lousy for engineering.

I am not going to delve into this debate. Since I have studied both linguistics and mathematics, I can easily see the strengths and weaknesses of both points of view, and this will help me decide when I should use Perl and when I should use Python.

Much of the material in Chapter 1, although interesting, is in my opinion unnecessary for a learning book. The entire section, “Is Python a ‘Scripting Language’?”, seems to me more appropriate for the Programming Python book. In my opinion, a learning book should get to writing code as quickly as possible, with philosophical meditations saved for another book.

Chapter 2 discusses how the Python interpreter works to run Python scripts. The chapter directs the reader to Appendix A, which contains instructions for obtaining and installing Python for Windows, Linux, or Unix. (Mac OS X 10.5 comes with Python 2.5.1 already installed.) The remainder of the chapter provides background information about the Python interpreter and can be omitted by the reader in a hurry.

Chapter 3 describes how to run Python scripts. It begins with teaching the reader how to start Python from the shell prompt in order to use Python’s interactive command line. The chapter patiently explains how to do this for each of the major operating systems and problems the reader needs to watch for. Then the chapter explains how to write a short Python script and invoke the script from the command line or by double-clicking its icon, and it explains in detail how to overcome the difficulties with double-clicking an icon in Windows. I found these details in Chapter 3 very useful, and it is clear that Mr. Lutz has learned from his training courses what difficulties the novice user will encounter when first using Python.

Chapter 3 continues with a brief introduction on importing modules using import and the differences between import and reload. The chapter concludes with an introduction to the IDLE user interface and other integrated development environments (IDEs).

Part I concludes with a set of very good exercises. The exercises get the reader started with using the Python command line, the IDLE IDE, and the Python documentation available from the command line and from the Python web site.

Resources:

April 05 2008 09:03 pm | Bioinformatics and Computing

Comments are closed.