IronPython = Python + .Net Framework

By Ben Hall, Microsoft Student Partner.

This article is going to discuss IronPython. IronPython is an open source project which has implemented the Python language on the CLR to work with the .net Framework allowing developers to take advantage of the .net framework with the dynamic nature of the Python programming language. 

The following code is an example of IronPython. This is the input and output from the IronPython interpreter (ipy.exe) which is included as part of the implementation. “>>>” means it is willing to accept a command, “…” means it is continuing on from the previous command, without this it is output from a previous command.

>>> message = “Hello World!”
>>> print message
‘Hello World!’
>>> message = 2 * 2
>>> print message
4

The code declares a variable called message which holds “Hello World!”, then the print command outs the contents to the screen.  We then redefine message with the result of 2 * 2 which is the value 4.  This dynamic declaration, runtime type assignment and other techniques, allows for greater flexibility than more static languages, like C# or Java.

With IronPython being developed on top of the CLR, it allows developers to take full advantage of the.net framework, or other pre-existing code written in other .Net languages, such as C# or VB.net, but also take advantage of Visual Studio 2005 for debugging. 

By default, IronPython only supports Python methods and functions allowing code to be ported from and to other python applications.  For example, doing the following will fail because Python strings do not have a Trim() method like CLR strings do.

>>> str = “ Spaces “
>>> str.Trim()
Traceback (most recent call last):
File , line 0, in <stdin>##10
AttributeError: 'str' object has no attribute 'Trim'

By entering import clr, it extends the objects and the language to support CLR methods.

>>> Import clr
>>> str = “ Spaces “
>>> str.Trim()
‘Spaces’

The following example demonstrates how IronPython code can create and show a WinForm, this takes advantage of the winforms class which comes as part of the tutorials which handles threading for winforms applications. First we execute the interpreter, import the tutorial’s class, we then select the namespaces we require, create a form object, show the form on the screen, set its caption bar text and add a label.

C:\IronPython\Tutorial>..\ipy.exe
>>> import winforms
>>> from System.Windows.Forms import *
>>> from System.Drawing import *
>>> f = Form()
>>> f.Show()
>>> f.Text = “My IronPython Form”
>>> f.Controls.Add(Label(Text = “Hello World!”))

This will show a form, with the text Hello World.  We can also add event handlers to the form, for example to display a message box when the form is clicked.

>>> def Clicked(sender, e):
... MessageBox.Show(“Clicked”)
...
>>> f.Click += Clicked

The def keyword defines a method, in this case Clicked which takes two arguments. 

Dynamic languages are becoming more popular with more enhancements coming over the next year.  I hope this has given you an insight into IronPython and dynamic languages.

You can obtain IronPython by visiting https://www.codeplex.com/IronPython.