Book Image

Python Essentials

By : Steven F. Lott
Book Image

Python Essentials

By: Steven F. Lott

Overview of this book

Table of Contents (22 chapters)
Python Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a reusable module


In Python, the module is the unit of software reuse. When we have a feature that must appear in more than one script, we'll put this feature into a module and import that module into each script that shares the feature.

It's important to note two slightly different senses of the word "reuse" as follows:

  • We can define a class hierarchy to achieve localized reuse within an application. Inheritance is an elegant way to share code among related objects. Often we'll define all of these related classes in a single module file.

  • We can define a module to achieve a less local reuse across applications.

To create a module that can be imported, we merely have to be sure that a Python file is visible in a directory that's part of the Python search path. Since the local directory is always visible, we can create a module simply by creating a file in the current working directory.

A module designed for import should consist mostly of import, class, and def statements. We can also...