ironpython Difference between Python and IronPython Using generics within IronPython


Example

IronPython enables to use generic classes and methods from the .net framework. Generics can be used with the same syntax as accessing an index. For passing more than one type-parameter, they must be separated with a comma:

l = Dictionary[int, str]()

That way we create a dictionary where keys only accepts integers and the values must be a string.

A sample usage could look like this

from System.Collections.Generic import List
lst = List[str]()
lst.Add('Hello')
lst.Add('World')
for l in lst:
    print

Output

Hello

World

When adding new items, type checking will also be performed:

lst = List[str]()
lst.Add(123)

Traceback (most recent call last):

File "<stdin>", line 1, in

TypeError: expected str, got int