PERFoRmINg ENUmERATIoNS
You’re going to encounter a lot of enumerations before the book is over because IronPython uses them
by the gross. Essentially, enumerating data means to obtain the individual elements from a collection
or array and do something with them. One of the more common collections that you’ll enumerate is
sys.path
. Listing 3-7 shows a simple example of how this process might work.
LISTINg 3-7:
Using an enumeration
import sys
# Enumerate the path variables.
continues
548592c03.indd 53
2/24/10 12:47:31 PM
www.finebook.ir
54
❘
CHAPTER 3
ImplementIng Structured language elementS
for ThePath in sys.path:
print ThePath
# Pause after the debug session.
raw_input(‘Press any key to continue...’)
As you can see, the
for...in
loop comes in very handy for enumerating data. Depending on your
configuration, you might not see all of the path information shown in Figure 3-5, but your output
will look similar. The “Changing sys.path Values” section of Chapter 2 tells you more about each of
the elements displayed in Figure 3-5.
FIgURE 3-5:
You can find many uses for enumerations in IronPython.
USINg IRoNPyTHoN CoNSTRUCTIvELy
This chapter has demonstrated the essential IronPython structures, including decision making and
looping. At first you’ll wonder whether you can get the task done with these simple setups. For example,
IronPython lacks a
Case...Select
structure. However, you can duplicate this kind of structure using a
simple
if...elif...else
data structure, so the complication of using
Case...Select
really isn’t nec-
essary. When you think about IronPython, think simple, but elegant.
If you’ve spent much time programming, you probably don’t need a lot of practice creating structures.
However, it’s important to spend some time reviewing the examples in this chapter because IronPython
can really throw a curve at you if you think it works like the other languages you use. Try creating a
few applications that have multiple levels to them so that you can see how indentation works within
IronPython.
Chapter 4 continues with more IronPython basics. In that chapter, you discover how to work with
arrays and collections. IronPython uses a host of arrays internally, so it’s important to pay particu-
lar attention to that part of the chapter. Just consider the number of times that arrays appeared in
Chapter 2 as the output of information. You’ll combine the information in Chapter 4 with the infor-
mation in this chapter to process arrays and collections.
LISTINg 3-7
(continued)
548592c03.indd 54
2/24/10 12:47:31 PM
www.finebook.ir
Do'stlaringiz bilan baham: |