Working with Generics

Must Read

Why every company should have a contingency plan – What You Need to Know!

What would you do if you have a call in the middle of the night with the news that...

How to Avoid a Second DUI Charge

Getting a DUI charge can feel like swimming deep into the ocean; you know you made a mistake, but...

Canon EOS 60D Set Your ISO

Digital photography has opened up a whole new world when it comes to setting the ISO speed on your...

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


# Add the .NET Framework 2.0 to the path.
import sys
# 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.
# Enumerate the list.
print ‘Initial List’
for I in MyList:
print I,
# Add a range of numbers.
# Enumerate the updated list.
print ‘nnList With Range Added’
for I in MyList:
print I,
# Remove a value that’s no longer needed.
# 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…’)


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.


Please enter your comment!
Please enter your name here

Latest News

Top 10 Provinces of Thailand Not to be missed for Travel

10 SuratthaniThe source of the proverbial Chaiya salted egg. Surat Thani is a province with many attractions,...

Underlight As Accent, For Power and The Main Light for Photography

Underlighting, in which fill or accent light comes from under the topic, is not widely used technique in the traditional Portraits, even though it will have...

How To Fix Overexposure As A Creative Tool, The Complete Guide

As an creative tool, overexposure is sort of underrated. What I’m close to propose could be a deliberate and well thought out technique for...

7D Mark II is Canon’s best DSLR cameras without full-frame sensor.

The expected long-awaited Canon EOS 7D Mark II are shipping in November for $ 1,799 without a lens. With a higher price tag, you...

Low key photography and How to isolate your subject.

Low key photography doesn’t depend on underexposure to make its point; the key to low key is that the majority of tones, even correctly...

More Articles Like This