Designing a module

In chapters two through nine of this book, we looked at a number of techniques for designing classes, which are the foundation of object-oriented design and programming. The module is a collection of classes; it is a higher-level grouping of related classes and functions. It's rare to try to reuse a single class in isolation. 

Consequently, the module is a fundamental component of Python implementation and reuse. A properly designed module can be reused because the needed classes and functions are bundled together. All Python programming is provided at the module level.

A Python module is a file. The filename extension must be .py. The filename in front of .py must be a valid Python name. Section 2.3 of Python Language Reference provides us with the complete definition of a name. One of the clauses in this definition is as follows:

"Within the ASCII range (U+0001..U+007F), the valid characters for identifiers are the uppercase and lowercase letters A through Z, the underscore _ and, except for the first character, the digits 0 through 9."

Operating system (OS) filenames permit more characters from the ASCII range than Python names; this extra OS complexity must be avoided. In particular, hyphens are a potential problem in Python module names; use underscores instead in complex file names. Because the stem of the filename (without the .py extension) becomes the module name, these names should also be valid Python identifiers.

The Python runtime may also create additional .pyc and .pyo files for its own private purposes; it's best to simply ignore these files. Generally, they're cached copies of code objects used to reduce the time to load a module. These files should be ignored.

Every time we create a .py file, we create a module. Often, we'll create Python files without doing much design work. This simplicity is a benefit of using Python. In this chapter, we'll take a look at some of the design considerations to create a reusable module.

Now let's take a look at some of the design patterns for Python modules.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset