58
❘
CHAPTER 4
Using ArrAys And ColleCtions
Tuples
and
list
s are more similar than different. For example, it’s possible to add elements to a
tuple
, just as you can to a
list
. In fact, except for the fact that
tuples
are immutable (not change-
able) and lists are mutable (you can change them), both structures provide precisely the same features.
Both of these structures are also roughly equivalent to arrays in other languages and you can call them
arrays without provoking too many odd reactions — at least, not from normal people.
Of course, you’re wondering now why Python and IronPython support two array-like structures.
Tuples
come in handy for a few reasons. First, because
tuples
can’t change, IronPython can imple-
ment them more efficiently, which means that using
tuples
is faster. Second,
tuples
are less suscep-
tible to outside influences such as viruses. Because you can’t change the content of a
tuple
, you also
can’t fill it with things that the application can’t use. Third,
tuples
are marginally faster to type.
The object that IronPython uses as an
array
really isn’t an
array
in the common sense of the
word. It’s more along the lines of a byte
array
for most programming languages. However,
the IronPython
array
really doesn’t fit that description either. Rather, you tell IronPython what
kind of sequence you want to store and then provide the storage values. Table 4-1 provides a list
of the
array
element types.
TAblE 4-1:
IronPython Array Data Types
dESIgNAToR
TyPE
SIzE
‘b’
Signed char (byte)
1
‘B’
Unsigned char (byte)
1
‘c’
Character
1
‘d’
Floating point (double)
8
‘f’
Floating point (single)
4
‘h’
Signed short
2
‘H’
Unsigned short
2
‘i’
Signed integer
2
‘I’ (capital eye)
Unsigned integer
2
‘l’ (lowercase el)
Signed long
4
‘L’
Unsigned long
4
As you can see from the table, an
array
in IronPython doesn’t include any concept of a string. Strings
in IronPython are considered a kind of sequence. To create an
array
of strings in IronPython, you’d
need to create an
array
of character arrays. In short, the concept of an
array
is somewhat primitive
in IronPython.
Arrays
also differ from other array-like structures in IronPython in that you must provide a type
as part of the
array
definition. Consequently, if you create a character
array
, you can’t suddenly
548592c04.indd 58
2/24/10 12:47:34 PM
www.finebook.ir
Do'stlaringiz bilan baham: |