Using IronPython Constructively ❘
137 At this point, the code makes some additions to
MyList
using the
Add()
method. Most .NET devel-
opers 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.
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.
uSINg IRoNPyTHoN CoNSTRuCTIvEly This chapter provides you with an introduction to working with the .NET Framework in
IronPython. Access to the .NET Framework is a huge advantage that IronPython possesses
that you won’t find with any other version of Python. This advantage limits you to the Windows
environment in most cases, unless you use a subset of the .NET Framework that also works with
Mono (
http://www.mono-project.com/Main_Page
). If you can find a way to work with just
the subset, your applications will also work without problem on both Mac and Linux so you
get the best of both worlds — full access to Python and full access to the .NET Framework in
one language. Amazing!
You’ve probably spent a good deal of time working with the .NET Framework when using other
languages. However, IronPython has specific needs and requirements when working with the
.NET Framework. It’s a good idea to spend some time working through examples of your own
548592c07.indd 137
2/25/10 9:44:31 AM
www.finebook.ir