aaron@traas.org

The home of Aaron Traas — man of faith, science, and very bad humor

As I've told numerous people over the years, a real Computer Scientist should be able to learn a new language in about a week. Maybe a bit more if it's a particularly strange language. But most imperative or OO languages should take about a week to learn for someone who is any good at the craft. Now, I'm not talking about mastery. Every language has its own set of idioms, tools, quirks, bugs, performance characteristics, etc. that take quite a bit longer to completely understand. I refer to, instead, basic competence, i.e., the ability to write functional, clean code, and reasonably ability to read well-written code in the language.

I took it upon myself to learn Python a couple months ago. I've been meaning to do so for years. I had just been introduced to Codeacademy, and I wanted to evaluate it so I could recommend it to other people wishing to learn how to code. It's very theory-light, but otherwise a good way to introduce someone to writing web-centric code. While I was there, I noticed the Python track, and decided to do it.

I was infatuated very quickly. As in, instantly.

First, I have a confession. Most of the programming I've done in recent years has been JavaScript and PHP. Both of them are pretty terrible languages in a lot of ways. Effective, but terrible. PHP in particular, though it's incredibly effective and performant enough, is really weird, has a powerful but often ill-conceived and inconsistent set of standard libraries, and has a lot of really odd behaviors in general. I have always loved it, however, for being a down-and-dirty language that doesn't get in your way for doing very simple things. Compare writing a simple email form in PHP to what you'd have to do in Java. In PHP, the whole thing can be done in one file that's mostly HTML, and has a dozen lines of (ugly and nasty) PHP code that does all of the heavy listing. In Java, you'd have to start a full blown project, make a couple of classes, set up a build system with Ant or Maven, build your WAR file every time you make a change, using dozens of files to do something that's ultimately very simple. Yuck.

It's no secret that I strongly prefer scripting languages for most small- to medium- scale web programming. Sure, there's cases where you want to build your RESTful API in C or Java for performance reasons, but I don't really see any point to building anything past the middle tier in a compiled language. If cacheing is used, you can make most front-end logic performant enough in even the slowest scripting language (coughRubycough), and it's also really easy to just through more hardware at the problem, which is typically cheaper than using more programmer time.

What I always liked about PHP is that it made string parsing and formatting really, really easy. Sure, it's a mess, and most PHP coders are terrible, but it's so simple to just get something working, fast, and with minimal code. Python, in most cases, is better than PHP at this. In some cases much better. In many cases, it requires even less code. Additionally, it encourages better coding habits.

Case in point: slicing. In PHP, getting the second through fifth characters in a string is pretty simple:

$my_string = "Hello world!";
$my_substring = substr( $my_string, 1, 4 ); // "ello"

Python really takes the cake with this one using a technique called slicing:

my_string = "Hello world!"
my_substring = my_string[1:5] # "ello"

Using slicing operations to replace string functions is just really nice, and makes for extremely readable, natural code. Plus, the same slicing operators work on lists (Python-speak for arrays) as well as strings.

The flow-control and looping structures in Python are simple, clean, orthogonal, and without any extra crap. Compared to PHP which has the really terrible C-style for and switch statements, Python is a breath of fresh mountain air.

One of the most valuable traits Python has over PHP is it's opinionated nature. When you want to do something in PHP, it's really hard to figure out what the best way is. There's usually a dozen or so options in PHP for any given operation that will yield a correct result. Python, in contrast is incredibly opinionated. It tells you (and enforces) the right way to indent code. It shows you the right ways to iterate through a list, the right way of handling string formatting, etc. It seems to have an opinion on everything, and even where it doesn't, the community does. This, of course, is not without its drawbacks, as I don't always agree with Python, but there's quite a uniformity in Python code online that doesn't exist in PHP

There's a few things in Python that seem a little clunky_—_mostly the different ways lists and dictionaries (read: hashmap or associative array) behave in for loops. I'd rather it always behaves as if you were using ennumerate(mylist) or mydict.iteritems(), as that seems to make the most sense. I'm personally not a fan of elif, preferring languages that provide the more natural and modular else if. I personally don't like whitespace being meaningful.

Where I actively dislike Python is its handling of object orientation, particularly the way you need to make a self parameter as the first (uncalled) parameter of every function. This makes it harder to read the code and see what member function signatures actually are to the outside world. Yes, I know that's how the this pointer in most languages actually works under-the-hood, but I don't think it's something that ought to be exposed in a very-high-level dynamic scripting language. It's just a messy implementation detail that I see as unnecessary.

Though PHP has a few strengths, Python a better language, full stop. It has a cleaner syntax, has a better community, is more enjoyable to code in, and has fewer unexpected behaviors. However, it's highly unlikely that I'll use Python any time soon for web programming.

Why? I primarily build web applications to order for clients. These live on their own server environments. All of my clients, right now, are running PHP, ASP.NET, Java, or a combination thereof. I'm not going to convince them to switch from PHP to Python. And why should they? PHP is performant enough, the hosting is cheap and readily available, and there are more PHP developers out there than Python developers (even if they are, on average, of lower quality). So I'm stuck with PHP for a while.

Which is why a lot of the trendy web developers really need to get off their high horses and shut the hell up. PHP is a practical solution for a lot of use cases, and it's most important trait is its ubiquity. It might not be the best language, it may not even be very good. But it is everywhere, and allows an awful lot of developers to be extremely productive.

Where Python has very quickly become my go-to language, however, is automation of simple tasks. I've already replaced a few shell scripts and ant tasks I depended on with much simpler and more featureful Python scripts. The fact that Python is installed by default on OS X and most Linux distros is really nice. I really, really wish Microsoft would include it by default with Windows; maybe they'd stop hemorrhaging web developers to the Mac if they made it a nice development environment out of the box like OS X is.

Keyboard Shortcuts

?
Show/hide help
Right
Select right-hand menu
Up
Next menu item
Down
Previous menu item
Left
De-select right-hand menu