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 2 - 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 - ' In Praise of Python '
( Page 2 of 6 )

I'm about to make a confession. Even though I've written several books and articles about C++, I have a secret: C++ isn't my favorite language. I have lots of languages that I use, each one for different purposes. But the language I consider my all-time favorite is Python.

Python is nothing short of beautiful. There aren't many languages where you can, for example, type one quick line of code to create a list that can hold items of absolutely any type you want. Here's the one line in Python:

a = []
ADVERTISEMENT

That's it. Isn't it beautiful? One little line of code that takes up four characters, not counting spaces. The list structure is built right into the language. Want to put an integer into the list? Here goes:

a.append(10)

How about adding a string?

a.append('python is great')

Now want to print it out? That's easy:

print a

...which gives you this:

[10, 'python is great']

What's that, you say? You want to add an instance of your own class? Sure, you can do that, too. Of course, now you have to get into some real programming. Let's make a class called MyClass. Here's a complete example:

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

a = []
a.append(10)
a.append('python is great')
a.append(MyClass('Sam', 80))

for item in a:
    print item

This code first creates a class called MyClass. I then declare two member functions inside MyClass. These two methods I declare are special operator methods; the first is a constructor, and the second returns a string representation of the class. Special methods get two underscores on either side of the names. My own methods don't have those underscores.

True, the language is a bit quirky at times. For example, Python relies heavily on indentation, which annoys some people. I could take it or leave it. But you can see the simplicity.

Here's another quirkiness: The first parameter of each member function is a reference to the object itself. (In C++, the same is true, except the member is implied; you don't explicitly type it. In Python you do explicitly type it.)

Notice the cool way I created a string in the __str__ function: I put a format string in quotes, using %s as a placeholder for each item. Then I follow the closing quote with a %, and then the variables filling in the placeholders inside parentheses, separated by commas. Very cool.

In this sample code, after creating the classes, I create a list structure, add an integer, add a string, and finally add a new instance of my class called MyClass. Next, I loop through the list, printing out each item. Here's the output:

10
python is great
Sam=80

Okay, that was the quick 30-second introduction to Python. But why are we really here? Because in addition to Python being my favorite language, I also have a favorite platform: Microsoft .NET.

Yes, I admit it. I love the open-source language of Python, yet at the same time I love .NET. And what's really cool? There's an implementation of Python that natively understands .NET. It's called IronPython, and you can get it by going to www.microsoft.com/downloads and typing IronPython into the search box.

At the time of this writing, IronPython is still in beta, so any link I give you will likely change. That's why I'm sending you to the general downloads site on Microsoft.



 
 
>>> 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.