Python considered Pretty Good

One of the things I’m spending spare minutes on while looking for a job is filling in gaps in my resume with skills that employers are asking for that I’m currently lacking. The main things I’m looking at are Puppet – a widely used Configuration Management system – Python, a scripting language that’s been around for some years, and Powershell, Microsoft’s recent foray into decent scripting languages. The fact that all 3 start with “P” is an interesting but meaningless coincidence.

Puppet looks very straightforward offhand; it uses a configuration model very similar to Nagios, which I’ve been using for years. Powershell  I’ve used somewhat previously, but don’t know enough to code extensive projects. However, I’ve mainly been looking at Linux based positions so Powershell is the last of the three I’ll be concentrating on.

That leaves Python.

I’ve had the first edition of O’Reilly’s “Learning Python” decorating my bookshelf for some years without really cracking the spine. Recently I picked up the 4th edition as an ebook and have been working my way through it. The book takes a LONG time to get moving. It spends at least fifty pages telling you good reasons to learn the language and how to fire up the scripting engine. However, I’m starting to get into the meat of it now.

I have to say I’m impressed.

For some time my default scripting language has been a bastard mishmash of Bourne shell and awk. Awk can be remarkably powerful due to its associative arrays; the feature was inherited into Perl and also (as it turns out) as “dictionaries”, Python.

Python has a remarkably powerful set of basic types. In addition to the standard integer, floating point, string, boolean and so forth, it has arrays (natch), dictionaries (associative arrays, indexed by object), sets (collections of objects with no duplicates) and tuples, which are basically constant arrays that can be used as indices in dictionaries and representatives of sets. There are also a bunch of standard types in the shipped libraries to handle fractions, complex maths and fixed-point arithmetic. And others (such as files and function references).

There’s also some very nice syntactic sugar constructs that can be applied to make code powerful while still remaining fairly readable.

The very powerful core idea, however, is the extent to which Python combines the idea of types associated with objects rather than variables, and operator polymorphism.

This means that a basic function can be redefined to operate with a different class as long as the class has operations corresponding to the operators used by the class. In a language like C or Perl, if you want to pick a maximum value, your function has to be defined for each class:

int max(int var1, int var2)
    return (var1>var2) ? var1 : var2;

This needs to be defined separately for each type, so you have the same basic code for integers, floating point, and any other relevant types. Generally speaking, it also needs to be defined for each combination of types (depending on how strongly the language is typed).

In Python the type is associated with the object, so as long as you have your operators overloaded with the right types, you can simply call the function with the relevant arguments. The function looks at the object types, picks out the function or code that performs the correct comparisons, and does the job. It’s an extremely powerful approach.

In any case, I’m still working my way through the book so it will be a while before I’m fully familiar with the language. Liking it so far, however…

(The title of this article is a very indirect reference to Dijkstra’s classic letter “goto considered harmful.” It’s been quite a while since I saw an actual goto in code, although modern languages have mechanisms that do similar things in a less horrible way.)

Leave a Reply

Your email address will not be published. Required fields are marked *