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

When an object type, known as a class, is provided in a module we must import the module (as we did with sys earlier, then prefix the object type with the module name to create an instance that we can store in a variable. We can then use the variable without using the module name.

We will illustrate this by considering a fictitious module meat which provides a Spam class. We import the module, create an instance of Spam and access its operations and data like so:

>>>import meat

>>>mySpam = meat.Spam() # create an instance, use module name

>>> mySpam.slice()

# use a Spam operation

>>> print mySpam.ingredients

# access Spam data

{Pork:40%, Ham:45%, Fat:15%}

 

Other than the need to create an instance, there’s no real difference between using objects provided within modules and functions found within modules. Think of the object name simply as a label which keeps related functions and variables grouped together.

Another way to look at it is that objects represent real world things, to which we as programmers can do things. That view is where the original idea of objects in programs came from: writing computer simulations of real world situations.

Neither QBASIC nor Tcl provide facilities for adding operators to complex types. There are however add on libraries for Tcl which allow this and the more modern Visual Basic dialect of BASIC does permit this.

Python Specific Operators

In this tutor my primary objective is to teach you to program and although I use Python in the tutor there is no reason why, having read this, you couldn’t go out and read about another language and use that instead. Indeed that’s exactly what I expect you to do since no single programming language, even Python, can do everything. However because of that objective I do not teach all of the features of Python but focus on those which can generally be found in other languages too. As a result there are several Python specific features which, while they are quite powerful, I don’t describe at all, and that includes special operators. Most programming languages have operations which they support and other languages do not. It is often these 'unique' operators that bring new programming languages into being, and certainly are important factors in determining how popular the language becomes.

For example Python supports such relatively uncommon operations as list slicing ( spam[X:Y] ) and tuple assignment ( X, Y = 12, 34 ). It also has the facility to perform an operation on every member of a collection using its map() function. There are many more, it’s often said that "Python comes with the batteries included". For details of how these Python specific operations work you’ll need to consult the Python documentation.

Finally, it’s worth pointing out that although I say they are Python specific, that is not to say that they can’t be found in any other languages but rather that they will not all be found in every language. The operators that we cover in the main text are generally available in some form in virtually all modern programming languages.

That concludes our look at the raw materials of programming, let’s move onto the more exciting topic of technique and see how we can put these materials to work.

32