Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Gauld A.Learning to program (Python)_1.pdf
Скачиваний:
23
Добавлен:
23.08.2013
Размер:
1.34 Mб
Скачать

Files

As a computer user you know all about files - the very basis of nearly everything we do with computers. It should be no surprise then, to discover that most programming languages provide a special file type of data. However files and the processing of them are so important that I will defer discussing them till later when they get a whole section to themselves.

Dates and Times

Dates and times are often given dedicated types in programming. At other times they are simply represented as a large number (typically the number of seconds from some arbitrary date/time!). In other cases the data type is what is known as a complex type as described in the next section. This usually makes it easier to extract the month, day, hour etc.

Complex/User Defined

Sometimes the basic types described above are inadequate even when combined in collections. Sometimes what we want to do is group several bits of data together then treat it as a single item. An example might be the description of an address:

a house number, a street and a town. Finally there's the post code or zip code.

Most languages allow us to group such information together in a record or structure.

In BASIC such a record definition looks like:

Type Address

HsNumber AS INTEGER

Street AS STRING * 20

Town AS STRING * 15

ZipCode AS STRING * 7

End Type

The number after the STRING is simply the maximum length of the string.

In Python it's a little different:

>>>class Address:

... def __init__(self, Hs, St, Town, Zip):

... self.HsNumber = Hs

... self.Street = St

... self.Town = Town

... self.ZipCode = Zip

...

That may look a little arcane but don't worry I’ll explain what the def __init__(...) and self bits mean in the section on object orientation. Some people have had problems trying to type this example in at the Python prompt. At the end of this chapter you will find a box with more explanation, but you can just wait till we get the full story later in the course if you prefer. If you do try typing it into Python then please make sure you copy the indentation shown. As you'll see later Python is very particular about indentation levels.

The main thing I want you to recognise in all of this is that we have gathered several pieces of data into a single structure.

30

Accessing Complex Types

We can assign a complex data type to a variable too, but to access the individual fields of the type we must use some special access mechanism (which will be defined by the language). Usually this is a dot.

To consider the case of the address type we defined above we would do this in BASIC:

DIM Addr AS Address

Addr.HsNumber = 7

Addr.Street = "High St"

Addr.Town = "Anytown"

Addr.ZipCode = "123 456"

PRINT Addr.HsNumber," ",Addr.Street

And in Python, assuming you have already typed in the class definition above:

Addr = Address(7,"High St","Anytown","123 456") print Addr.HsNumber, Addr.Street

Which creates an instance of our Address type and assigns it to the variable addr. We then print out the HsNumber and Street fields of the newly created instance using the dot operator. You could, of course, create several new Address type variables each with their own individual values of house number, street etc.

The Tcl way

In Tcl the nearest approximation to complex types is to simply store the fields in a list. You need to remember the sequence of the fields so that you can extract them again. This could be simplified a little by assoigning the field nu,mbers to variables, in this way the example above would look like:

set HsNum 0 set Street 1 set Town 2 set zip 3

set addr [list 7 "High St" "Anytown" "123 456"]

puts [format "%s %s" [lindex $addr $HsNum] [lindex $addr $Street]]

Note the use of the Tcl format string and the nested sets of '[]'s

User Defined Operators

User defined types can, in some languages, have operations defined too. This is the basis of what is known as object oriented programming. We dedicate a whole section to this topic later but essentially an object is a collection of data elements and the operations associated with that data, wrapped up as a single unit. Python uses objects extensively in its standard library of modules and also allows us as programmers to create our own object types.

Object operations are accessed in the same way as data members of a user defined type, via the dot operator, but otherwise look like functions. These special functions are called methods. We have already seen this with the append() operation of a list. Recall that to use it we must tag the function call onto the variable name:

>>> listObject = []

# an empty list

>>> listObject.append(42)

# a method call of the list object

>>> print listObject

 

 

[42]

 

 

31