Mimsy Were the Borogoves

Hacks: Articles about programming in Python, Perl, PHP, and whatever else I happen to feel like hacking at.

Learning Python

Jerry Stratton, March 1, 2006

I rarely follow “learning” books sequentially through from start to finish. I generally read the first chapter or two and then skip around while writing some software that I need to write. Learning Python had me all the way through to the end, mostly because Python has significant differences in style and philosophy than C, Perl and PHP.

I originally wanted to learn Python because I manage the Mailman mailing list software at the University. Mailman is written in Python and we occasionally need to customize it. I bought Programming Python several years ago, and it was useful enough for that purpose, but it didn’t help me learn Python enough to write my own scripts and software from scratch. Nor did it tell me why I might want to.

Python’s advantages

I tend to look at scripting languages very practically: what can they do for me now? I currently use Perl and PHP most of the time. The advantages of Python for me are clearly defined scope, easy objects, named variables in function calls, and to some extent partial pre-compilation.

In Python, everything that gets defined in a file is only available in that (or from that) file. There is none of the namespace pollution that you see in languages such as PHP or Perl where everything has access to everything else.

When I wrote this journal software several years ago, I used PHP because it was the best choice at the time. Were I to write it today, I would seriously consider Python’s scope against PHP’s ability to be easily embedded inside text documents such as HTML pages.

For example, when I add plug-ins to this software, I always prefix function names with a code to ensure that the functions do not conflict with other functions. You see this in plug-ins for other journal/CMS software as well, and even then you are hoping that nobody else uses the same code. Since it is usually a two-letter abbreviation it is a serious issue.

In Python having two functions with the same name in different files doesn’t matter. If I have a plug-in called “forum” and a plug-in called ”shoutback” and each has a function called “displayMessage”, the journal’s engine will call the one as “forum.displayMessage()” and the other as “shoutback.displayMessage()”.

The same applies to incidental globals: globals are per file, not per application. You control which globals are available by which files you import, and globals are referenced in the same way that functions are: by prefixing them with the file’s name.

For large projects, such as, I suspect, Mailman, Python’s scope rules are a huge advantage.

Python also pre-compiles as needed. Whenever a module (file) is used, Python compiles it to “byte code”, which is much faster to parse than the source code on demand. As long as Python has write access to the folder that contains the module, that byte code version is then written out. From now on, until the original file is modified, Python will use the byte code version rather than the source code version. “The net effect is that pure Python code runs somewhere between a traditional compiled language, and a traditional interpreted language.”

As far as objects go, just about everything in Python is an object, in the sense that they contain methods and properties. If you want the length of an array, you use “arrayname.length”, not “length(arrayname)”. If you want to convert a string to title case, you’ll use “stringname.title()” rather than, say, “ucwords(stringname)”.

Sometimes the choice of what contains which methods is a little strange; if you want to implode an array into a string separated by a comma, you’ll use “','.join(arrayname)” not “arrayname.join(',')”. But in general, the heavy objectification in Python works well.

An easy tutorial

I purchased Learning Python during the 2005 Emerging Technologies conference here in San Diego, and was able to follow it easily during free time at the conference. I rarely go through tutorials as deeply as I went through this one. I’ll usually give up after a few chapters and just start some project. Part of it was that Python does so many things differently that it is necessary to keep following the tutorial just to know what questions to ask the index. But part of it is that the tutorials are well written; each step builds on the previous step, but it doesn’t go so slowly as to bore. It isn’t only easy, it’s interesting.

After going through the tutorials and after starting to use Python for some projects, I’ve found that the book continues to be useful as a reference.

Overloading

Strings are, in a way, used as an example of how to overload basic functions on variables. Many of the basic mathematical expressions are overloaded for strings, for example. Besides using the “+” to concatenate strings, you can multiply a string by 3 to repeat the string three times. You can overload these things yourself, for your classes.

Unique data types

Besides the standard data types (data objects) such as numbers, strings, lists, and dictionaries (associative arrays), Python has a few unique ones as well, such as complex numbers and tuples.

Tuples are a sort of immutable group of objects. They work very much like lists, but have their own special uses, some of them, according to the book, historical.

Complex numbers are those square roots of negative numbers that you might recall from high school algebra. For example, “print 4j*4j” results in “-16”. More specifically, it results in “-16+0j”: the real portion is -16, and the imaginary portion is 0.

Even functions and methods can be used as data objects: they can be placed in lists, assigned to variables, and passed around in pretty much the same way any other data type can.

Loops

Python does away with the standard C-style for counter. Python’s “for” is like PHP’s “foreach”: it loops through items in a list. There is a “range()” function to provide numbers for a counter. It does so by providing a list of numbers that are then iterated through by “for” the same as for any other list of items.

Example: Role-playing game assistant

So what might you do with Python? In my case, I use it for scripting simple applications that are easier to script than write a program for, but which I want Python’s scope and other application-useful features.

For example, I might want to make a script that rolls ability scores for a role-playing game. So I could create a class like this: