I recently began working with the Python programming language. Python has been on my to-learn list for at least a year, but without a project to use as a vehicle (or enough free time to justify inventing one) I didn't get around to it. Luckily, one of my tasks at work last week was to prototype a websockets server for an app we're working on. I started with Ratchet because we typically use PHP for server-side work and Ratchet looked like the best of the PHP websockets frameworks. And it worked, up to the point when I began implementing a session validation handshake between the client, the websockets server and our REST API, which manages user authentication on a separate server.

PHP has been the target of a lot of justifiable criticism of late, but one of its weak points that doesn't get as much attention is its synchronous design. Want to make an asynchronous request to another server and get the response in a callback? Good luck with that. Sure, you can recompile PHP with ZTS enabled and try to get pthreads running, but that's the kind of long and painful process that should make you reconsider whether you've chosen the right tool for the job. It didn't take long to come to the conclusion that PHP was a bad choice. Each synchronous request from the websockets server to the REST API blocked all other activity on the websockets server, rendering it useless for realtime communication.

Having ruled out PHP, I reviewed my options for server-side languages with built-in support for asynchronous operations (there aren't many) and decided to give Python a shot. Python's reputation as a flexible and programmer-friendly language seems to be deserved - within a day I had a basic websockets server up and running with Tornado. I have spent a few days since then getting comfortable with Python's syntax and semantics while refining the server and forming my first impressions of the language. In short, I like it. A lot.

After nearly a decade writing JavaScript, one of the few good things I can say about it is that it's flexible, relatively easy to learn and great for rapid prototyping. Python shares those qualities, but doesn't appear to suffer from the awful design decisions that plague JavaScript. Python's design seems to be directed by a well defined philosophy and the result is a very concise and consistent language.

Python eschews braces and line endings as a means of delimiting blocks of code in favour of indentation. A for loop that would be written like this in PHP…

for ($i = 0; $i < 10; $i++) {
    echo $i;

…would look something like this in Python:

for i in range(0, 10):

While rewriting my websockets server in Python, I was typically able to accomplish the same tasks with less code than the PHP version, and the resulting code was cleaner and easier to read simply by virtue of Python's design.

Python supports subclassing and class methods, features I'm used to from Objective-C and often miss in PHP and JavaScript. It also supports class variables, which I haven't encountered in any other language.

Python runs on Mac OS X, Linux and Windows, so it's a good choice if you care about portability. OS X and most Linux distributions include it as standard (open Terminal, type python and press Enter, and you'll launch a Python interpreter that you can use to run simple commands without writing a script) and Windows installation is easy.

I'm still very new to Python, but I expect it will gradually replace PHP as my server-side language of choice and quickly become my go-to for many general programming tasks. It's flexible, consistent, portable, fast and fun.