DCSIMG
Python for .Net - Shai Raiten

Shai Raiten

 Subscribe

Python for .Net

Python for .Net

Python for .NET is available as a source release and as a Windows installer for various versions of Python and the common language runtime from the Python for .NET.

Here is a small tutorial for getting started:

Importing Modules

from System import String
from System.Collections import *
import clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import Form

Using Classes

from System.Drawing import Point
p = Point(5, 5)

Using Generics

from System.Collections.Generic import Dictionary
from System import *
dict1 = Dictionary[String, String]()
dict2 = Dictionary[String, Int32]()
dict3 = Dictionary[String, Type]()

Fields And Properties

from System import Environment
name = Environment.MachineName
Environment.ExitCode = 1

Using Indexers

from System.Collections import Hashtable
table = Hashtable()
table["key 1"] = "value 1"

Using Methods

from System import Environment
drives = Environment.GetLogicalDrives()

Overloaded and Generic Methods

from System import Console
Console.WriteLine.__overloads__[bool](true)
Console.WriteLine.__overloads__[str]("true")
Console.WriteLine.__overloads__[int](42)

Delegates And Events

def my_handler(source, args):
print 'my_handler called!'
# instantiate a delegate
d = AssemblyLoadEventHandler(my_handler)
# use it as an event handler
AppDomain.CurrentDomain.AssemblyLoad += d

Exception Handling

from System import NullReferenceException
try:
   raise NullReferenceException("aiieee!")
except NullReferenceException, e:
    print e.Message
    print e.Source

Using Arrays

from System import Array
myarray = Array[int](10)

Using Collections

domain = System.AppDomain.CurrentDomain
for item in domain.GetAssemblies():
   name = item.GetName()

Type Conversion

items = System.Array.CreateInstance(Point, 3)
    for i in range(3):
        items[i] = Point(0, 0)
    items[0].X = 1 # won't work!!

Enjoy

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: