Visualize History

How it all went down.

Why use Python over PHP? And why Django?

Commenter Darrin asks “what are the benefits/drawbacks of Python (or Django in this case) vs. PHP?” 

There are two separate issues at hand here, which language to use and which framework to use, given the language.  I choose Python over PHP for two main reasons which are really historically the same reason. 

Why not PHP

PHP rapidly spread across the web after its creation.  One reason it developed so quickly was its ease of use.  In those days, a file on the server corresponded to a file that the user saw.  If I went to http://strassdesign.com/pictures.php, the web server would load /strassdesign.com/public_html/pictures.php and parse it, displaying the result of the PHP code.  Any code outside the PHP tags (<?php and ?>)would be output as HTML.  Page design closely modeled the imperative style of programming familiar to any C programmer, and since most programmers knew C in those days, people understood the metaphor.

People did not however, want a version of C for the web, since C is so impossibly complex for new or amateur programmers.  PHP adopted a more JavaScript-like attitude of forgiveness.  For instance, for a long time, the default settings allowed direct reference to variables passed in via GET, which meant any user could get a variable set in your script simply by appending ?name=value to your .php url.  Of course “it just works” is fine until it doesn’t, and then finding a bug is nearly impossible.  Ask anyone who has done JavaScript development.  Still, for a while, “it just works” was all people needed.  In those days, web software could easily be modeled as

  1. make a request to the server
  2. process input data and return html
  3. view html

As web software became more complex, key concepts in modern programming became important, with the most important being object oriented coding.  For those not familiar with the distinction, object oriented programming allows for encapsulation and modularity, among other things.  That’s a fancy way of saying the code I write for my user logins shouldn’t have easy access to the code I write for the poll, or for my archives, or for any other discrete piece of my site.  PHP of course added object orientedness to the language, starting in PHP3 and more completely in PHP5.  As an old and well adopted language, though, a lot of tutorials and example code relied on the imperative nature of the language.  More importantly, though, the language wasn’t designed to be object oriented, so basic concepts of objects require a fairly complex and non-intuitive syntax and implementation.  I don’t usually like to talk about the “feel” of a language, but in this case, PHP couldn’t feel less object oriented.

Why Python

In school I took a parallel programming course in which we used Python, and I loved it.  The concepts made sense to me, and after a while I used Python for all my scripting needs.  I like its GUI interpreter IDLE, and the ability not to have look up every function online by using the dir( ) command.

Python’s most important advantage is that it was designed to be object oriented.  Modules are fundamental to Python, and almost no fancy functionality exists outside a module.  For example, PHP uses built in functions like xml_parse for XML but Python uses modules, which means I can switch or add formats without needing to find the correct function in the core language.  I’m not sure I could explain the advantages of object oriented programming without adding another 500 words, but if you are interested, I will gladly chat about pros and cons. 

For web development, there are really 3 popular non-Microsoft scripting languages: PHP, Python and Ruby.  Having eliminated PHP, I had to choose from the other two.  Since I was already fairly expert in Python, I chose to explore that as an option.  It already solved my biggest problem with PHP, namely the proper use of objects.

Why Django

Python may be the coolest thing since sliced bread, but it was not designed for the internet.  I had to make sure that I could map web concepts like 404 pages, dynamic urls and GET/POST requests to Python.  Django does exactly that.  I thought Django might be difficult to learn at first, but it uses the feel of Python well.  Its modules follow common practices in Python, and it took me 45 minutes to learn the necessary parts of the framework. 

So I knew it could work, but I had to make sure it solved my other problem with PHP, namely the separation of code and presentation. Programmers don’t often make good designers, as Joel, Jeff, and Joel and Jeff all explain.  These days it is unusual for a designer and a coder to be the same person.  I am both in my little project, of course, but even then I don’t wear both hats at the same time.  When I’m in the zone coding, I don’t want to think about <div> or styles.  When I’m designing, I tolerate thinking about those things, as well as colors and such nonsense.  So for me, that separation is absolutely key.  Django provides a simple templating system that supports inheritance in a way that makes perfect sense for the web.  All my pages will have the same meta info, the same header and footer, and some other stuff, and I certainly don’t want to write those more than once.

Django provides some added benefit that I didn’t know I needed at the time, but now love.  The first is its database access, which abstracts away SQL and instead uses Python objects to represent data.  I hate writing and debugging SQL, and for the most part, I don’t need the power and complexity that relational databases provide.  All I ever do is add objects, read objects, and find related objects.  Once you write one JOIN statement, you don’t need to write any more, ever again.  So I don’t. 

The second feature surprise was the URLConfs, which map logical URLs to code.  I can now have a page like http://www.visualizehistory.com/exhibits/featured map to a function, which is completely transparent to the user.  If I ever want to change the functionality, it is trivial to do so.  It also lets me manage the URLs of the site in the same way a human thinks of them, that is conceptually and not as pieces of the filesystem I happen to have.

The last feature of Django I rely on heavily is the built in web server.  It lets me develop offline, make a lot of changes quickly and generally speeds up development time a lot.  The server is simple enough that I can use it and complicated enough that it mirrors my actual set up in the cloud.

4 comments

4 Comments so far

  1. Darrin Dickey January 25th, 2009 10:48 pm

    Thanks for the overview. Python (or more properly Django) sounds interesting.

  2. Maria August 30th, 2009 7:11 pm

    I never ever post but this time I will,Thanks alot for the great blog.

  3. Python November 9th, 2009 6:18 am

    You have my vote on Python+Django too. I’m a former PHP programmer and when I now compare coding in Python/Django to my previous coding experience I must say that I made a good decision.
    Also worth to note that Python allows to create GUI applications, while in PHP it isn’t really possible.

  4. B Pliska December 15th, 2009 2:04 pm

    Kohana MVC php5 strict framework. Pure OOP.

Leave a reply