Getting Started with IronPython
The Dynamic Language Runtime (DLR) is a framework for creating dynamic languages that run on the Common Language Runtime (CLR). At the Lang.NET symposium the DLR team made quite a splash with
IronPython. IronPython version 1.0 was released in late 2006 and then the DLR was abstracted out. The DLR is still in beta, along with IronPython 2, which is built on top of it. IronPython 2 and the DLR share a release schedule with Silverlight 2, and will be out of beta by the end of the year.
IronPython v1 is already an excellent implementation of Python, passing most of the Python test suite and able to run large Python applications like
the web framework Django.
There is increasing developer interest in dynamic languages, and Python is a particularly good one. It is widely used, semantically concise and easy to learn. As a dynamic language, types are enforced at runtime rather than compile time. Developers used to statically typed languages may find this removal of the "safety net" worrying, but in practise it turns out not to be a problem, particularly if you are using best practises for your testing cycles. There is a performance cost for these runtime checks, but dynamic typing makes it easy to do things that are either cumbersome or not even possible with statically typed languages. Benefits include first class (and higher order) functions and types, late binding, easy introspection, duck-typing and metaprogramming. For more on dynamic typing, read
Strong Typing versus Strong Testing.
The most important thing about IronPython is how well it is integrated with the .NET framework, and how easy it is to work with managed objects. This makes mixing C# and IronPython very straightforward. Beyond writing full applications, other practical uses for IronPython include embedding it into applications to provide a scripting environment, using it as a standalone language for system administration and utilising it as a tool for exploring assemblies. This last use case is best done with the interactive interpreter, which is also a great place to start experimenting.
After downloading IronPython, you start the interpreter by running "ipy.exe", which you can also use at the command line to execute Python scripts. You will be presented with a version string and a 'prompt', at which you can enter code and 'interactively' see the results. This means that you can inspect objects to see what methods are available and what happens when you call them. You start by adding references to assemblies (at runtime of course). Here's a simple example using
System.Management:
IronPython 1.1 (1.1) on .NET 2.0.50727.1433Copyright (c) Microsoft Corporation. All rights reserved.>>> import clr>>> clr.AddReference("System.Management")>>> from System.Management import *>>> mo = ManagementObject("Win32_PerfFormattedData_PerfOS_Processor.Name='_total'")>>> for p in mo.Properties:... print p.Name, '=', p.Value
...
For more recipes illustrating how to work with IronPython, visit the
IronPython Cookbook. However, the best resource for IronPython, is my book "
IronPython in Action".
Michael Foord, Senior Software Engineer at
Resolver Systems.
Read
Michael's Blog.