Working with Generics

0
125

Generic classes are exceptionally easy to work with in IronPython, which is a good feature to have because the .NET Framework includes a lot of generic classes. Listing 7-5 starts with a simple List, but the technique shown in this listing works with every other generic class that .NET provides.

Listin g 7-5: Using .NET generics

[code]

# Add the .NET Framework 2.0 to the path.
import sys
sys.path.append(‘C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727’)
# Make clr accessible.
import clr
# Import the required assemblies.
import System
import System.Collections.Generic
# Create an integer list.
MyList = System.Collections.Generic.List[int]()
# Add items to the list.
MyList.Add(1)
MyList.Add(2)
MyList.Add(3)
# Enumerate the list.
print ‘Initial List’
for I in MyList:
print I,
# Add a range of numbers.
MyList.AddRange(range(5))
# Enumerate the updated list.
print ‘nnList With Range Added’
for I in MyList:
print I,
# Remove a value that’s no longer needed.
MyList.Remove(4)
# Display the results.
print ‘nnList With 4 Removed’
for I in MyList:
print I,
# Remove a range of items.
MyList.RemoveRange(1, 2)
# Display the results.
print ‘nnRemoved Items 1 and 2’
for I in MyList:
print I,
# Pause after the debug session.
raw_input(‘nPress any key to continue…’)

[/code]

The code begins with the usual imports. Notice that you must import System.Collections.Generic to make this example work. If you were going to work quite a bit with this namespace, you’d want to import it into the global namespace because typing the longer namespace would prove time consuming and error prone. Fortunately, you use it only once in this example, to create MyList, which is an int List. Notice that the data type of List appears within square brackets and that the example uses a Python data type. You could just as easily use a .NET data type.

At this point, the code makes some additions to MyList using the Add() method. Most .NET developers have used this technique since they first started working with their .NET language of choice, so there aren’t any surprises here. The initial output appears in Figure 7-15.

Figure 7-15: You can use generics to interact with a list.
Figure 7-15: You can use generics to interact with a list.

The next addition might surprise you a little. The code uses the AddRange() method to add a range of numbers supplied by the range() function. This is another example where you can easily mix .NET and Python code without any problems. Figure 7-15 shows that MyList now contains both the initial additions and the range of numbers.

You can also remove values from MyList. The first example uses Remove() to remove a specific value. The RemoveRange() method removes a range of the entries by position. Both removals appear in Figure 7-15 so you can see their effect.

Like many of the techniques described in this chapter, List has a lot more to offer. Make sure you perform a dir(MyList) to see other tasks you can perform with the List generic class. For example, you might want to see the results of using Reverse() or Sort() on MyList. Have some fun with this example because discovering what generics can add to IronPython is important.