Main Content

Moving from scripting to Object Orientation in Python

Archive - Originally posted on "The Horse's Mouth" - 2011-09-13 07:39:28 - Graham Ellis

Using what's been described as the scripting idiom, a "scripter" will write a series of code steps which perform a task from top to bottom. For a job that only involves simple data manipulation (even if there's a lot of data to have that manipulation applied), this approach can be very effective indeed - a short script, easy to follow, and with no dependencies on other bits of code.

Here's the start of a Python example from yesterday:

  fh = open("railstats.xyz")
  for station in fh.xreadlines():
    parts = station.split("\t")
    print parts[1],parts[3],parts[9],parts[6]


The code works well enough, and for a once-only job it's acceptabel and perhaps the most efficient way of coding - but how many jobs are truely "once only"? Notice in my example how specific separators and column numbers from the data file end up in the main program code, making it harder to debug and harder to follow than had it been written like this:

  fh = open("railstats.xyz")
  for place in fh.xreadlines():
    current = station(place)
    print current.getcode(), current.getosref(), \
      current.getyear(2007), current.getname()


It's now ...
• much clearer what the fields are
• independent of the field separator
• able to hide minute details of the particular column numbers

Of course, that's not the full story. I've still had to provide the code that defines the splitting character, names each of the fields, etc - it's in a class called station and I'm writing using the Object Oriented idiom. And that does where I just have a single simple use to make of a data set lead to code that totals more lines. But, look, I also gain ...
• A data handler that can be reused in other programs
• A data handler which is easily tailored if I want to use it on a somewhate different data set
• A data handler which can be independently maintained and tested


On yesterdays's Python Course, I wrote the two pieces of code above as parts of complete examples showing the "scripting" and Object Oriented approaches - you can find the complete source of the OO example [here] and the complete source of the scripting example [here]. The full data file to accompany the demonstration may be downloaded from [here]. The scripting approach may be considered to be a bit of a "bull at a gate" approach, hence the illustration ... which was derived from public domain resources at www.public-domain-image.com.

Technically, a scripting language is one in which each line is translated from source as it's run - and with the languages we teach that's really just Tcl and Shell these days. However, the term has become (ab)used to mean a language that's run direct from source code, all be it through an intermediate compiled form interally and an informal (or perhaps formal) virtual machines. Note added for the purists ;-)




Reuse of classes!. Here comes some of the power - a day later, I reused the same class for a sorting demo ... source code [here].