Languages - DevSource
DevSource: Microsoft Developer Resource DevSource Home Sponsored by Microsoft Home Add Ons Architecture Languages Techniques Using VS Forums
Home arrow Languages arrow Page 3 - A First Look at IronPython: Where Python meets .NET
A First Look at IronPython: Where Python meets .NET
By Jeff Cogswell

Rate This Article: Add This Article To:

A First Look at IronPython: Where Python meets .NET - ' Python Meets '
( Page 3 of 6 )

.NET">

Python Meets .NET

If you're familiar with both Python and .NET, then you might immediately recognize some troubles. For starters, Python has built-in types, such as the list structure I was just bragging about, which don't immediately match up to a built-in .NET type.

ADVERTISEMENT

Fear not; all that is covered. But first, let's try out an example.

IronPython comes with a console program that runs just like the Python console. After you install IronPython, you can open a command prompt, add IronPython's root directory to your path, and type ironpythonconsole. (Yeah I know, it's long. They could do better on that.)

Sometimes, the IronPython console takes a little while to start up, because the .NET framework needs to load. But once it does, you can type Python code. You have three >>> characters as a prompt. Here's a sample session:

C:\>ironpythonconsole
IronPython 1.0.2378 (Beta) on .NET 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
>>> a = 'hello'
>>> b = 1
>>> b + 3
4
>>> a.upper()
'HELLO'
>>>

The sample code I gave in the first section runs perfectly, yielding the exact same output as it does with regular Python. To try it out, save it to a file with a .py extension (I called mine demo1.py) and run it by typing import demo1.py at the IronPython prompt:

>>> import demo1
10
python is great
Sam=80
>>>

Not bad! But what's really going on here? Let's think this through: IronPython is, in fact, a big program written in C#. Is it just the Python interpretter ported to C#? No. If that were the case, I'd just be running a Python interpretter that knows nothing about .NET, an interpeter that just happens to have been written in a .NET language. But that's not the case.

IronPython allows you to both create Common Language Runtime (CLR) classes, and to take advantage of the .NET framework. In other words, IronPython is a Python language interpretter that includes .NET support. Cool.

So what's going on with the MyClass class I just created? Is it just a class, or is it also a CLR class? By default, the class is not automatically a CLR class. But it's easy to make it into one. Just derive it from the base of all .NET classes, System.Object. To do that, you have to import the namespace. Here's a quick sample that you can paste into the IronPython console:

import System

class MyClass(System.Object):
    def __init__(self, name, height):
        self.name = name
        self.height = height
    def __str__(self):
        return "%s=%s"%(self.name, self.height)

b = MyClass('Angie', 66)
t = b.GetType()
print t.FullName

This code creates the same class as before, but derives it from System.Object. Then the code creates an instance, storing it in the variable b. Next the code calls GetType. But what is GetType? That was inherited from the System.Object class, and is present in all .NET classes. It returns an instance of the .NET class Type, of which I can call FullName to get the full name of my .NET type.

Here's what I see when I run this code:

IronPython.NewTypes.System.Object_1

Aha! Proof positive! My class called MyClass is, in fact, a .NET type, and it lives in some strange namespace called IronPython.NetTypes.System. But what's the final thing? Object_1. That's the real name of my type as it lives inside the CLR.



 
 
>>> More Languages Articles          >>> More By Jeff Cogswell
 



Microsoft's Future: A Chat With Their CTO, Barry Briggs

Play Video >

All Videos >

Julia explores the Robotics Studio!

Read now >

Messages to Bill Gates!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.