Why Python rocks.

It’s probably pretty clear Python is my language of choice.  What I haven’t made clear is why. There are lots of reasons, including no compile step,  clean, readable, but powerful syntax… the choice between OOP and procedural programming. (While OOP is generically better, to quick-test a bit of code or concept, a much faster procedural test is awesome.).

Also awesome, is the fact that it’s been ported to two managed languages. Java (Jython), and .NET(IronPython). Now, you may be wondering why you’d port one programming language to another. The short form, is A: As a scripting language, and B: To access the libraries written in Java and .NET through Python. Basically, IronPython makes Python a .NET language (I.E, on level with C# or VB.NET), and Jython means python can access Java libraries and functions. mod_python is awesome if you’ve got a fresh Apache set up, but if you’ve already got a whole bunch of Java servlets… well, fitting in a few Python servlets to either prototype with, or make use of python’s exceptional agility, starts to look really appealing. 

Which brings me to the last point of Python’s Awesomeness. A live console. I’d reccomend installing python anyway, but if you want to get the idea first, go here. You’ll have to approve a security warning, as it’s an appalet running a full Python console via Jython. It’s on GNUCitizen, but if you’re the paranoid type, source is available here.

And probably the best way to get used to a new language is to play around with it a little. So here’s a quick primer.

First, the basics. (Just type this into the interactive console, hit enter to go to a new line, no real worries.

A simple math test.

print 2 + 2

This is building a list.

list = [1,2,3,4,5,6,7,8,9,0]

Printing the list.

print list

Look, an iterator!  (Just hit enter after the colon, then hit enter twice after the n.)

for n in list:
	print n

Basics out of the way, lets look at something a little more substantial (This only works in Jython, as it refrences Java code.) A more-or-less linear translation of Sun’s Swing hello world. (Put into procedural form to make it easier to type out into the console.)

from javax.swing import *
frame = JFrame("Hello, Jython!")
label = JLabel("Hey, a label!")
frame.getContentPane().add(label)
frame.pack()
frame.setVisible(1)

At this point, a Java window should pop up. Now, if you would, window your web browser with the console in it, and arrange it so you can see both the new Java window, and the python console, and type this line.

label.setText("Look, Dynamic!")

When you hit enter, notice anything? Like, how the label changed instantly, without any need to recompile, relaunch, or otherwise fiddle with it? You can do the same thing with WinForms in IronPython, and Tkinter in stock (CPython). (Though, the procedure in Tkinter is different). Overall, it provides a really agile and quick way to develop. Plus, the syntax is dead simple.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: