Tim Hatch

Weblog | Photos | Projects | Panoramas | About

Python? Say What? 03 Aug, 2005

I was first introduced to Python about three years ago by someone who worked at the College of Business with me, and fulfilled the much-disdained position of “Perl maintainer.” Moe was an advocate of doing things, basically, “the right way or not at all,” and this, he explained, was why we should rewrite Windows RegEdit in Python to make it scriptable. I’m not making this up, that was an actual project that came up at COBA.

Well, Python went on the back burner (relative to PHP, which was more applicable to business-type needs, and therefore useful to COBA) until the beginning of this summer when I started researching it further. Many projects I like to use I found out were written in Python (Gentoo’s emerge, Bittorrent, and more recently, Django), so I checked into it after advocating a number of smaller-platform languages, such as Ruby (which I got my brothers into, but without an appreciation of the complexity of C, the simplicity was lost), and Objective-C (which is a fun language, and I love the syntax, but the documentation is a little too lax for me to look things up).

In fact, Obj-C is proably the closest I’ve seen in terms of languages to compare Python to. In a way, it’s similar to most C-derivative scripting-type languages, like Perl and PHP, but in Python, everything is an object, or at least looks like it’s in an object due to the way modules are imported.

In PHP, I felt like the syntax for classes didn’t really encourage people to use them. In Python, classes are almost like mini-namespaces which allow grouping of related functions (while I do dislike the verbose self parameter in class functions, it’s something I got used to, and not nearly as annoying as Perl’s “parameter passing” (hrumph) scheme).

Why switch?

Lately, I’ve been writing a lot of console scripts to do various things. The language I’ve used for most of them is PHP, and for the most part, it’s a severe case of everything looking like a nail. Some things are a bit difficult in PHP (for example, you need to explicitly open stdin, and zipping arrays — alternating elements from two different arrays to fill a new one — is hard). It also occasionally has some really stupid warnings (fread throws a warning with bs=0? cut me some slack!) and some things are coded (IMnshO) wrong (feof(invalidhandle) returns false? And false==0?). One of the great things I loved in Obj-C was the fact that there’s a thing like null, except it’s not (in Obj-C, that was Nil), which exists in Python as None (with implementation differences, like you can send messages to Nil, and they’ll return Nil, which perpetuates what’s likely an error, whereas Python will toss an exception ASAP).

Python is the most perfect prototyping tool I’ve found so far for the commandline. Perl is a mess, unmaintanable, BASH is difficult to work with (2d arrays anyone?) and PHP is, to be honest, the wrong tool for the job.

I was able to develop a quick script to parse through PNG files this morning, which I refactored into a class in about five minutes, counting design time. Documentation? It’s actually fun to add in docstrings in Python, for some weird reason. I can’t explain it, but I feel compelled to add in a string to each function just for the heck of it. And rather than remembering some obscure syntax, it’s a human-readable string, which is auto-parsed out into the special doc member.

I then tried to translate the code to PHP after it became apparent that this script was useful to other people (who might not have Python), and probably spent longer dealing with PHP issues than it took me to write it in Python, period. This was even with my previous port of argparse to simplify commandline handling.

Python is my friend. And, another added bonus, it aint’ Perl.

Comments