Building a Module

As you saw in Chapter 7, modules provide a convenient way to share Python code between applications. A module is a very simple construct. In Python, a module is merely a file of Python statements. The module might define functions and classes. It can contain simple executable code that's not inside a function or class. And, best yet, a module might contain documentation about how to use the code in the module.

Python comes with a library of hundreds of modules that you can call in your scripts. You can also create your own modules to share code among your scripts. This chapter shows you how to create a module, step by step. This includes the following:

Exploring the internals of modules

Creating a module that contains only functions

Defining classes in a module

Extending classes with subclasses

Defining exceptions to report error conditions

Documenting your modules

Testing your modules

Running modules as programs

Installing modules

The first step is to examine what modules really are and how they work.

Was this article helpful?

0 0

Post a comment