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

More information on the Address example

Although, as I said earlier, the details of this example are explained later, some readers have found difficulty getting the example to work. This note gives a line by line explanation of the Python code:

The complete code for the example looks like this:

>>> class Address:

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

... self.HsNumber = Hs

... self.Street = St

... self.Town = Town

... self.Zip_Code = Zip

...

>>>Addr = Address(7,"High St","Anytown","123 456")

>>>print Addr.HsNumber, Addr.Street

Here is the explanation:

>>> class Address:

The class statement tells Python that we are about to define a new type called, in this case, Address. The colon indicates that any indented lines following will be part of the class definition. The definition will end at the next unindented line. If you are using IDLE you should find that the editor has indented the next line for you, if working at a command line Python prompt in an MS DOS window then you will need to manually indent the lines as shown. Python doesn't care how much you indent by, just so long as it is consistent.

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

The first item within our class is what is known as a method definition. This method is called __init__ and is a special operation performed by Python when we create an instance of our new class, we'll see that shortly. The colon, as before, simply tells Python that the next set of indented lines will be the actual definition of the method.

... self.HsNumber = Hs

This line plus the next three, all assign values to the internal fields of our object. They are indented from the def statement to tell Python that they constitute the actual definition of the __init__ operation.The blank line tells the Python interpreter that the class definition is finished so that we get the >>> prompt back.

>>> Addr = Address(7,"High St","Anytown","123 456")

This creates a new instance of our Address type and Python uses the __init__ operation defined above to assign the values we provide to the internal fields. The instance is assigned to the Addr variable just like an instance of any other data type would be.

>>> print Addr.HsNumber, Addr.Street

33

Now we print out the values of two of the internal fields using the dot operator to access them.

As I said we cover all of this in more detail later in the tutorial. The key point to take away is that Python allows us to create our own data types and use them pretty much like the built in ones.

Points to remember

Data comes in many types and the operations you can successfully perform will depend on the type of data you are using.

Simple data types include character strings, numbers, Boolean or 'truth' values.

Complex data types include collections, files, dates and user defined data types.

There are many operators in every programming language and part of learning a new language is becoming familiar with both its data types and the operators available for those types.

The same operator (e.g. addition) may be available for different types, but the results may not be identical, or even apparently related!

34