Importing Modules - Python Made Easy (2013)

Python Made Easy (2013)

Chapter 7: Importing Modules

“From the viewpoint of what you can do, therefore, languages do differ - but the differences are limited. For example, Python and Ruby provide almost the same power to the programmer.”- Yukihiro Matsumoto

In this chapter you will learn:

· About Modules

· How to use modules

· How to import modules

In the last chapter we covered classes which allow you to declare multiple functions and then call to them as needed thorughout the code. These combinations of variables and functions that can be called to form a nice, neat package which can make them much easier to deal with.

What Are Modules?

Now, what are modules? They are generally definitions of variables, functions, and classes. Think of them as the next rung on the ladder. Multiple classes can be contained within functions, and multiple functions can be contained within modules. A module looks very similar to any other Python program that you might code, and often contain large portions of code that might be called to throughout a larger program.

Modules were created because when you quit the Python interpreter and then attempt to enter it again, your created functions and variables that were read by the interpreter will be lost. When writing a longer program this can be a very serious issue. The longer your program gets– the more you want to separate the blocks of code so that you know where to access those blocks and can use them several times throughout your program.

Modules are files that contain Python definitions and statements. They are typically appended with the suffix .py, which you may have to use when calling to the file. The name of the module (a string) is available as a global variable.

Defining Modules

Modules are different because they allow you to import all of the module, or bits of the module into other programs. Create a new file named moduletest.py, and then input the following code; Your average module might look like this;

# Define variables:

numberone = 1

ageofgrandma = 79

# define some functions

def printhello():

print "hello"

def timesfour(input):

print input * 4

# define a class

class baseballCard:

def __init__(self):

self.brand = raw_input("What brand is the card? ")

self.player = raw_input("What player is on the card? ")

self.price = raw_input("How much did it cost? ")

self.age = raw_input("How old is it (in years)? ")

def printdetails(self):

print "This card is of " + self.player + ",

print self.brand, "card, of " + self.player, "is“ + self.age +“years old and costs

" + self.price + " dollars."

Looks pretty familiar right? It looks like any old Python program that you might code. Now, you can import specific sections of code from any module, which can then be used within your programs. In order to import portions of code from a module, you must use an import operator. If you wanted to important an entire module, it is relatively simple;

### mainprogam.py

### IMPORTS ANOTHER MODULE

import testmodule

Keep in mind that in this example, we are assuming that the module is located in the same directory as the mainprogram.py file. But, you can also import very specific segments of your program as well. This is great for when you want to call on a specific class or variables that are contained within your modules.

Import statements are most commonly contained within the beginning of a Python file, but can technically be found anywhere within a program. Here is an example of calling on specific portions of code contained within a module;

### USING AN IMPORTED MODULE

print testmodule.age

baseballCard = moduletest.baseballCard()

baseballCard.printdetails()

This is a straightforward example of how you can call on various aspects of a module from your main program. You can import either your whole module, or very specific parts of the module. You can even call on global values of a module, and assign them to your program locally. An example of this would be;

# Assigning to a local name

timesfour = moduletest.timesfour

This assigns the value given by the“timefour” function and assigns it to a local variable. This is great for when you want to take global variables and give them a local assignment. This can make variables easier to call to, without having to call the module as a whole.

Python modules should be relatively simple for you to understand at this point. Modules provide you with yet another organizational technique that give you more control over your program as a whole. You can use them to create increasingly complex programs, without feeling overwhelmed by the amount of code that you are creating. Splitting programs up into classes, functions and modules makes it easier to organize your code and gives you a consistent way to call to certain blocks of code that may need to be used throughout your program.

PROJECT: Modules

Use our previous example as a guide. Create a module that stores information about movies. One class, titled“movieInfo” should contain relevant information about the movie including the name, director, star, runtime (in minutes) and price. Have these values stored using raw_input. Also create a function that then prints those details.

Answer:

# define a class

class movieInfo:

def __init__(self):

self.name = raw_input("What is the name of the movie? ")

self.director = raw_input("What is the name of the director? ")

self.star = raw_input("Who stars in the movie?")

self.runtime = raw_input("What is the runtime of the movie? ")

self.price = raw_input("How much did the movie cost? ")

def printdetails(self):

print self.name + self.director + self.star + self.runtime + self.price

Summary

The following points were covered in this chapter:

Modules

What are modules

How to use modules

Defining modules