Log in / Register
Home arrow Computer Science arrow Data Structures and Algorithms with Python
< Prev   CONTENTS   Next >

1.6 Importing Modules

In Python, programs can be broken up into modules. Typically, when you write a program in Python you are going to use code that someone else wrote. Code that others wrote is usually provided in a module. To use a module, you import it. There are two ways to import a module. For the drawing program we are developing in this chapter, we want to use turtle graphics. Turtle graphics was first developed a long time ago for a programming language called Logo. Logo was created around 1967 so the basis for turtle graphics is pretty ancient in terms of Computer Science. It still remains a useful way of thinking about Computer Graphics. The idea is that a turtle is wandering a beach and as it walks around it drags its tail in the sand leaving a trail behind it. All that you can do with a turtle is discussed in the Chap. 18.

There are two ways to import a module in Python: the convenient way and the safe way. Which way you choose to import code may be a personal preference, but there are some implications about using the convenient method of importing code. The convenient way to import the turtle module would be to write the following.

from turtle import * t = Turtle()

This is convenient, because whenever you want to use the Turtle class, you can just write Turtle which is convenient, but not completely safe because you then have to make sure you never use the identifier Turtle for anything else in your code. In fact, there may be other identifiers that the turtle module defines that you are unaware of that would also be identifiers you should not use in your code. The safe way to import the turtle module would be as follows.

import turtle

t = turtle.Turtle()

While this is not quite as convenient, because you must precede Turtle with “turtle.”, it is safe because the namespace of your module and the turtle module are kept separate. All identifiers in the turtle module are in the turtle namespace, while the local identifiers are in the local namespace. This idea of namespaces is an important feature of most programming languages. It helps programmers keep from stepping on each others' toes. The rest of this text will stick to using the safe method of importing modules.

1.7 Indentation in Python Programs

Indentation plays an important role in Python programs. An indented line belongs to the line it is indented under. The body of a function is indented under its function definition line. The then part of an if statement is indented under the if. A while loop's body is indented under it. The methods of a class are all indented under the class definition line. All statements that are indented the same amount and grouped together are called a block. It is important that all statements within a block are indented exactly the same amount. If they are not, then Python will complain about inconsistent indentation.

Because indentation is so important to Python, the Wing IDE 101 lets you select a series of lines and adjust their indentation as a group, as shown in Fig. 1.5. You first select the lines of the block and then press the tab key to increase their indentation. To decrease the indentation of a block you select the lines of the block and press Shift-tab. As you write Python code this is a common chore and being able to adjust the indentation of a whole block at a time is a real timesaver.

Fig. 1.5 Adjusting Indentation in Wing IDE 101

Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
Business & Finance
Computer Science
Language & Literature
Political science