Python VS PHP

Aah, the old programming language debate, much akin to comparing religions or political parties – bound to end up in a fist fight. Some will argue that as long as you choose the right tool for the job, it doesn’t matter, but I’ve found it’s always the little things that break the camel’s (or in this case, the programmer’s) back.

I wrote a little Python pet project (mind the pun) this week after years of web development using only PHP. As a matter of fact, my first job was a Python position. Eventually, as I started doing GUI development in Delphi, Python was relegated to odd jobs and hacks. When I started doing PHP development, Python became redundant and eventually my skills became rusty as old nails. Until now.

Even after just one week using Python, I remember why I’ll always prefer it – it empowers me as a programmer. In the Python community, they refer to it as the “batteries included” philosophy – with the mere inclusion of a library, anything becomes possible. Combine that with clean and readable syntax, fluent interfaces, powerful list comprehensions, list slicing, an interactive console, and too many other features to mention, it’s clear to see why.

I’ll illustrate using a Python example (Yes, it’s a bad example):

date = details[1].split("<td>")[1].split("</td>")[0].strip()

This line takes the second element of a list (the Python equivalent of an array) and parses a date string out of an HTML table cell. Whether or not it is advisable to do this is a topic for another day, but it illustrates quite handily how the combination of “everything is an object”, great list operators and string functions makes a hack like this effortless. Here is the equivalent PHP code:

$parts = explode('<td>', $details[1]);
$otherParts = explode('</td>', $parts[1]);
$date = trim($otherParts[0]);

If you’re a PHP developer and haven’t worked with Python before, do yourself a big favour and head over to the Python tutorial or check out any of the multitude of hacks, projects and frameworks written in Python and see how it’s the little things that make programming enjoyable.

Leave a Reply

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