Sunday, January 24, 2010

Python is amazing

If you are just starting out with web design, have HTML, CSS, JavaScript and basic uploading/hosting stuff down you want to take the next step and venture into server side scripting. This allows you do do simple things, like create an email form, or complex things, like build a whole forum. You can connect databases, create files on your sever on the fly, create your own tracking code...lots of things.

Now, the current convention is to go with PHP, and really, PHP is easy. It is actually easier to learn PHP than what it is to really learn CSS and JavaScript. And it can do cool things. Just check out this amazing tutorial at Tuxradar - http://www.tuxradar.com/practicalphp. If all you want to do is simple scripts, it is going to work just fine for you.

But what if you want a little more power. You want to pull your blogger posts in to your website, show the weather, create a custom Twitter api. You can do many of those things with PHP. No problem. Often each peace of the puzzel is easy. So why am I raving about Python.

Well, first of all, Python goes beyond the web. It is a scripting language, or a 'high level' language, but that doesn't mean it doesn't have some Vooma! It can build great GUI apps, small database scripts are so easy with Sqlite3. There is so much you can do with it. And you can talk to the web with it. You can download sites, pull them apart and re-build them.

But, you say, look at the popuarity of PHP. It is huge on the web. Well, sure. It is. But I bet some of the time it is underestimated how big Python is. You don't often see Python programmers having a website with a .py ending...mainly because of the whole way that Python programmers think. Python has clean syntax and Python programmers want a clean URL as well.

But, the main reason is that it is rather easy to build small PHP script. Really easy. It must be the easiest server side scripting language to do things with.

My argument, then, for using Python more is actually not so much about writing the code in the first place. That's complex. You have to really cover a lot of documentation to figure out how to use Python with your website. With PHP, it tells you how on the very first page of most tutorials.

No, the reason I love to work with Python is that most work I have done is done in bits and peaces. I have some time, start a nice big project, then put it aside when I go on tour or do some work for others. When you come back to your PHP, trying to wade through the code is tough. It thought me the importance of comments. Python loves comments. Mmmm, good Python.

When you leave a peace of code, once it gets a little involved, it is always a challenge to come back and jump into it again. But one of Guido's key insights in his design of the language was the code was read far more times than it was written. So make it easy to read. And he did exactly that.
Ref 

If you are thinking of trying to write a blog, or even better, have a few blogs of your own and want to pull them together, add login scripts, add Twitter, weather and what not, I am sure you will find Python is easier to maintain in the long run. One main feature that makes it better than PHP is namespaces, as is stated in the last line of PEP 20:


The Zen of Python,
    by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!