Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Introducing IronPython

IronPython is an easy-to-learn yet surprisingly powerful language for .NET development. Find out how it differs from C# and Visual Basic while still leveraging your existing .NET knowledge.


advertisement
ack before version 1.0 of the CLR shipped, Microsoft engaged a variety of commercial and academic organizations to produce languages that ran on .NET; an effort code-named "Project 7." One of those languages was Python for .NET, developed by ActiveState. That worked, but Project 7 discovered that the "speed of the current system is so low as to render the current implementation useless for anything beyond demonstration purposes.1" Furthermore, while they blamed some of the performance problems on "the simple implementation of the Python for .NET compiler", they also claimed that "[s]ome of the blame for this slow performance lies in the domain of .NET internals and Reflection::Emit".

Largely due to ActiveState's experience, it became conventional wisdom that "[t]he CLI is, by design, not friendly to dynamic languages.2" This conclusion caught the attention of Jim Huginin, the original creator of Jython—an implementation of Python for the Java VM. Given that Jython runs reasonably well on the JVM, Jim wondered why the CLR ran Python so poorly. He decided to take a couple of weeks and build a simple implementation of Python on .NET in order to determine what Microsoft had done wrong. His plan was to use the findings to write a short paper titled "Why .NET is a Terrible Platform for Dynamic Languages."

Jim was shocked to discover that his two-week effort resulted in a Python implementation that ran faster than Jython. Intrigued, he kept working on IronPython, eventually joining Microsoft in the summer of 2004. The 1.0 version of IronPython shipped two years later, in September 2006. Today, Jim is an architect in the Dynamic Language Runtime team while the IronPython team is driving towards a fall 2008 release of IronPython v2.0.

Getting IronPython
The IronPython project lives on CodePlex. Unlike most other Microsoft language products, IronPython runs as a transparent, open source project. The IronPython team releases a new version approximately every six weeks. Furthermore, Microsoft makes all the source code available under the OSI approved Microsoft Public License. On the CoDePlex site you'll find a mailing list for community discussion as well as a public issue tracker where anyone can submit bugs they find.

The currently released version of IronPython is v1.1.1; however, the IronPython team is hard at work on v2.0. As I write this, we have just released Beta 3 of IronPython v2.0. IronPython v2.0 implements the 2.5 version of the Python language (IronPython v1.x implemented Python v2.4). Furthermore, IronPython v2.0 is built on the new Dynamic Language Runtime.

Regardless of which release you download, IronPython releases are distributed as a simple zip file containing the compiled binaries, some tutorials, documentation, and the usual readmes and license files. You can simply unzip that archive to any location on your hard drive. Inside the unzipped files you'll find the main IronPython executable, ipy.exe. If you run that with no arguments, you get an interactive console window where you can start typing Python code directly. Alternatively, you can pass the name of a python file as the first argument to ipy, and IronPython will execute that code.

Despite the availability of a robust version of IronPython, there's no current production-quality development experience for IronPython in Visual Studio. However, the VS Extensibility SDK includes a sample IronPython language service, project system and console window, which has now been packaged as "IronPython Studio"—a free download from CodePlex. Improving the Visual Studio experience for IronPython developers is one of the IronPython development team's areas of investment for the future.

Significant Whitespace
When you first start working with Python, the most notable difference from other languages is the use of significant whitespace. In C#, whitespace is entirely insignificant—statements end with a semicolon and scope blocks are delineated with curly braces. VB, on the other hand, uses the end of line to indicate the end of a statement. However, scope blocks are still explicitly terminated in VB, typically with the keyword End (i.e. End Sub, End Function, End Class), although blocks sometimes close with a term more relevant to the block being closed, such as (Do/Loop, For/Next, etc).

In contrast, Python uses significant whitespace both for statement termination as well as delineating scope blocks. Like VB, Python terminates statements at the end of the line. Python uses a colon to explicitly mark the start of a scope block, similar to the way C# uses the open curly brace; however, unlike VB or C#, Python uses indentation to implicitly determine the end of a code block. All code with the same level of indentation is part of the same code block. To demonstrate, here's an implementation of the bubble sort algorithm written in Python:

def bubble_sort(ar): l = len(ar) - 1 for i in range(l): for j in range(l-i): if ar[j] > ar[j+1]: temp = ar[j] ar[j] = ar[j+1] ar[j+1] = temp return len(ar)

This code has four scope blocks: the bubble_sort function itself, the two for loops (Python's for loop is the equivalent of C#'s foreach loop and the range function is the equivalent of Enumerable.Range), and the if block. Each block has a different level of indentation. For example, you can see that the return statement is at the same level of indentation as the first for loop, indicating that it's not part of either the for or if scope blocks, but belongs to the function scope block. This example uses four spaces for indentation, but the specific amount of indentation is irrelevant, as long as it's consistent.

Significant whitespace enables both consistency and readability. In C-syntax languages such as C#, programmers have differing opinions as to where to put braces, using tabs vs. spaces, when to indent and how much, etc. Because C# ignores whitespace, there are numerous ways to format your code. In Python, there's only one legal way to format the code. This means that if you walk up to a random bit of Python code written by some person you've never met, it'll be formatted exactly in the same style that the Python code you write is. This typically makes Python code more readable. The fact that you don't need extraneous syntax (semi colons and curly braces) to indicate the ends of statements and scope blocks also improves readability.

Python's significant whitespace is one of those things developers tend to either love or hate. However, the use of significant whitespace does appear to becoming more popular. Both Boo and Cobra, two open source .NET languages, use a Python-inspired syntax, including significant whitespace. Furthermore, Microsoft's new F# language includes an optional lightweight syntax mode that makes indentation significant, just like Python.

Editor's Note: This article was first published in the September/October 2008 issue of CoDe Magazine, and is reprinted here by permission.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap