Sunday, April 12, 2015

Python... and a letter to an editor

Python, the programming language, seems to be in the news more than usual. Or perhaps it's just the "Python For Beginners" article in Communications of the ACM that got me thinking. The article seems unreasonably critical for one ostensibly promoting the language. And so, for the first time, I've written a letter to the editor of that esteemed journal. More on that in a moment.

If I think through all of the technologies that have made my life easier and better, the first one that comes to mind is, quite naturally, the Internet. The second is Python, created by Guido van Rossum. Allow me to reminisce for a moment.

Thanks, Paul!
Back in the nineties my programmer and brewmaster friend Paul introduced me to Python. His enormous enthusiasm engraved the incident in my mind. But my life had little room for a new language at the time. I was primarily doing database programming in a 4GL and using the horrific accident known as Perl for server scripting.

I am sure that the first program Paul showed me was about four lines long and sent emails automatically. It might have looked like this:

import smtplib
server = smtplib.SMTP('hostname', 25)
server.login('username', 'password')
server.sendmail('me@here.com', 'you@there.com',\
"I just thought I'd write to tell you about Python.")

I was impressed. Later I came across three-line web servers and other examples of how useful a higher level language can be. This might be more a testament to the comprehensive built-in library than the language itself. But the code itself was incredibly transparent, more like a pseudo-code that happened, by some sort of magic, to run as intended.

I used to characterise Perl as a write-only language. It is cryptic, designed for high priests of code who never have to share with anyone else. Python is named after Monty Python, made for people who love beer, and who like to share. I like the spirit and the ethics behind such a project, almost as much as I love being able to get my work done in record time.

Though it has been years since I made my living coding, I still keep Python on hand for all sorts of daily tasks. My backup regimen is run through Python, as is my bulk file naming. I trust both of these tasks to my code more than I would to any third-party application.

The most amazing thing about the language is how I can pick it up after months away from coding. I whip up a short programme, or even one of several hundred lines. And it. Just. Works.

I'll give an example in my next blog post, but first, back to that letter to the editor. I will reproduce it here, in case it never otherwise sees the light of day.

Dear editor:

Esther Shein's article "Python For Beginners" downplays the robust nature of Python, perhaps in an attempt to maintain journalistic "balance". But the word count dedicated to critique seems misplaced in an article that could otherwise present more positive examples of usage.

The repeated reference to Python as a "scripting language" is dismissive. Python is not only useful for scripting applications or OS functions, but for industrial applications with tens of thousands LOC. I once led a development team that wrote a robust web transaction B2B infrastructure in Python, which handled individual database files containing millions of records. It's still running two decades later.

Python is strongly dynamically typed. That's what allows it to handle flexible runtime situations efficiently. If you want the advantages of static typing, that can be bolted on using a pre-processor like "mypy". You should also implement automated testing, structured documentation, and other safe-coding practices. But this is true no matter what the language, and in any case goes beyond the target audience of "beginners".

Shriram Krishnamurthi complains that Python has limited support for data structures. Yet it has built-in tuple, list, set, array, and dictionary types. Queues, b-trees, ordered dictionaries, named tuples, and so on are part of the standard library. Third-party libraries exist for every other imaginable situation.

For custom mixed data structures, use a class. Krishnamurthi bemoans their "onerous syntax and tricky semantics", which might be a reasonable criticism once one gets beyond trivial cases. But for simple structures all you need is the "pass" command. An example:

class Album:
    pass
favourite = Album()
favourite.name = 'Revolver'
favourite.artist = 'The Beatles'
favourite.year = 1966

This code can be understood by a novice once the instantiation at line 3 is explained. It's hardly "onerous". From here "put" and "get" methods, type checking, validation, and so on can be added in an incremental way. This approach eminently suits beginners.

The problem is perhaps one of bringing biases from one language into another. The lack of "struct" or static types does not make Python any the less capable. Perhaps the opposite!

Of course there are domains where some other programming language will be preferable. But Python is the language for the 99%.

Addendum
I've found the original article online here.

RELATED POSTS

No comments:

Post a comment