Wednesday, June 23, 2010

OO Hello World - IronPython



Let’s have a look at the hello world program in IronPython; the first language to run on the DLR runtime.

By the way, you can see my previous post here: http://carlosqt.blogspot.com/2010/06/oo-hello-world.html
where I give some details on WHY these "OO Hello World series" samples.

Version 1 (Minimal):
The minimum you need to type to get your program compiled and running.

class Greet(object): 
 name = ''
 def __init__(self, name):
  self.name = name.capitalize()
 def salute(self):
  print 'Hello ', name, '!'

# Greet the world!
g = Greet('world')
g.salute()

Version 2 (Verbose):
Explicitly adding instructions and keywords that are optional to the compiler.

import System

class Greet(object): 
 name = ''
 def __init__(self, name):
  self.name = name.capitalize()
 def salute(self):
  print 'Hello ', self.name, '!'

# Greet the world!
def main():
 g = Greet('world')
 g.salute()
 
main()

The Program Output:









IronPython Info:
“IronPython is an open-source implementation of the Python programming language which is tightly integrated with the .NET Framework. IronPython can use the .NET Framework and Python libraries, and other .NET languages can use Python code just as easily.” Taken from: (http://www.ironpython.net/)

Appeared:
2006
Current Version:
Developed by:
Jim Hugunin
Creator:
Jim Hugunin
Influenced by:
Python (Guido van Rossum)
Predecessor Language
Predecessor Appeared
Predecessor Creator
Runtime Target:
DLR
Latest Framework Target:
4.0
Mono Target:
Yes
Allows Unmanaged Code:
Yes
Source Code Extension:
“.py”
Keywords:
31
Case Sensitive:
Yes
Free Version Available:
Yes
Open Source:
Yes
Standard:
No
Latest IDE Support:
Visual Studio 2010 (pro, shell, etc + IronPython Tools)
SharpDevelop 3.2/4.0 (beta)
MonoDevelop 2.2
Language Reference:
Extra Info:

8 comments:

  1. Line 2 in simple and lines 1, 2 & 5 in verbose are entirely superfluous...

    ReplyDelete
  2. Added to which, line 2 in verbose (the import * form) is actually bad practise... (As well as unneeded if you import the System namespace directly - which is itself unneeded in that example because you don't *use* the System namespace.)

    Allows unmanaged code is wrong - unless I misunderstand your intent. IronPython now *includes* as standard the ctypes module, which is an FFI specifically to allow access to unmanaged code.

    ReplyDelete
  3. IronPython can do unmanaged code easily. Just use clrtype.py to create a DllImport attribute decorator.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. @Michael Foord

    First of all, thanks for your comments. I agree with you that lines 1, 2 and 5 are entirely superfluous, however, my intent is to show the closest program as possible in all the languages mentioned in one of my previous posts "Most Active .NET and JVM Languages", said that I will try to define the variable name even if there is no use on that just to make it clear and other people new to the language understand.

    Also, I try to follow the same program structure I mention on the "OO Hello, World!" post:

    [Import / using / etc] <--- even if not needed
    Class
    Data <--- name = '' even if it is not needed
    Constructor
    Method
    Program
    Instantiate Class
    Call Method => OUTPUT (Hello, World!)

    ReplyDelete
  6. @Michael Foord and @Jimmy

    Thank you for the clarification on "Allows unmanaged code"
    I will update the post to reflect it can handle unmanaged code.

    The intent of Allow Unmanaged Code is to say that there is support on the language to allow UNSAFE and pointers syntax on the language itself and not that it allows to use COM or Unmanaged through DllImport. Nevertheless it is true that you can interface with unmanaged code so I will update :) thx again.

    ReplyDelete
  7. Carlos - thanks for the reply.

    Perhaps rather than just yes / no for "Allows Unmanaged Code" you could have no / through library / builtin syntax to distinguish the different ways it is supported.

    Glad to see the "import *" go and I understand your motivation for keeping the other "superfluous" lines. :-)

    ReplyDelete
  8. you are welcome,

    I like the "Allows Unmanaged Code" by type idea. I will update previous posts and use it in the next ones.

    bytes!

    ReplyDelete