Tuesday, April 03, 2007

When is the last time you had fun programming?

Ask yourself that question. Be honest with the answer.

If your answer is something similar to, "It's been a long time since I had fun programming," you owe it to yourself to check out the Python programming language.

Even though I have my doubts about dynamically typed programming languages in general, I have to admit that learning to program in Python, and programming in Python, is just plain fun. It's easy to get started: Just download and install Python, and hop into the interactive interpreter. There's an excellent Python tutorial to help you get started, and even a free, high quality online book.

The Python interactive interpreter is a big part of what, for me, makes Python so much fun. It reminds me of programming BASIC on the Commodore 64 in the early 1980s. The Python interactive interpreter gives you that kind of instant gratification. (With suitable apologies to anyone under 30 years old, who probably has no idea what I'm talking about.)

For example, let's say you're starting to learn about Python lists. Instead of flipping back and forth between an editor and running your Python program to check the results, you can experiment with Python lists right in the interactive interpreter. Here's an example:

>>> mylist = ['one', 'two', 'three', 'four']
>>> print mylist
['one', 'two', 'three', 'four']
>>> mylist[1] = 'TWO!'
>>> print mylist
['one', 'TWO!', 'three', 'four']

This kind of experimentation in the interactive interpreter saves a lot of time. It's also a good way to learn how different functions behave in Python. Here's another example:

>>> myfile = file("hello.txt", "r")
>>> line = myfile.next()
>>> print line
hello world
>>> myfile.close()

You can even ask the interactive interpreter for help on objects and methods:

>>> help(file.close)
Help on method_descriptor:

close(...)
close() -> None or (perhaps) an integer. Close the file.

Sets data attribute .closed to True. A closed file cannot be used for
further I/O operations. close() may be called more than once without
error. Some kinds of file objects (for example, opened by popen())
may return an exit status upon closing.

The standard Python library is also quite comprehensive. You'll find support for just about anything you might need to do. For anything not built into Python by default, you can probably find a Python library that does what you need.

The community support for Python is impressive, too. If you have a question about Python, type your question into Google and you're likely to get a relevant result in your first search results page. You can also ask the friendly people at comp.lang.python on Usenet (though, admittedly, some of them are misguided and solidly in the "Python can do no wrong!" camp, or the "Why would you want to do that anyway?" camp -- you can identify and ignore them pretty easily, though).

In case you're worried or wondering, Python is plenty buzzword compliant. Dynamically typed, garbage collected, object oriented, etc. It's also very portable: You can run Python on your Java Virtual Machine (thanks to Jython) and on your .NET runtime (thanks to IronPython), as well as running the standard Python interpreter. Projects are well underway and making nice progress that enable you to compile Python to native code, as well.

If your corporate software development has you singing the blues, and you feel like bringing back some of that magic you used to feel when writing software, give Python a test drive. The worst that can happen is you waste a few hours, and the best that can happen is you'll discover a new, favorite programming language.

2 comments:

Unknown said...

Spend a few weeks doing "meetings with IBM", "document the process", "update the designs", "put together the deliverable", and "troubleshoot performance issue" THEN ask yourself

When is the last time you had fun programming?

After all that I entered a line of Java code and it was damn near orgasmic. I fear trying Python, the pleasure might put me into a coma.

Rick Kimmel said...

Python is on my list. Just can't seem to find time at home and I can't find a justification for it at work (PERL is the standard here).