|
|
||
Style NotesModules are a critical organizational tool for final delivery of Python programming. Python software is delivered as a set of module files. Often a large application will have one or more module files plus a main script that initiates the application. There are several conventions for naming and documenting module files. Module names are python identifiers as well as file names.
Consequently, they can only use "_" as a punctuation mark. Most modules
have names that are mixedCase, beginning with lowercase letters. This
conforms to the useage on most file systems. MacOS users would do well
to keep their module names to a single word, and end with
Some Python modules are a wrapper for a C-language module. In this
case the C/C++ module is named in all lowercase and has a leading
underscore (e.g. A module's contents start with a docstring. After the docstring comes any version control information. The bulk of a module is typically a series of definitions for classes, exceptions and functions. A module's docstring should begin with a one-line pithy summary of the module. This is usually followed by in inventory of the public classes, exceptions and functions this module creates. Detailed change history does not belong here, but in a separate block of comments or an additional docstring. If you use CVS to track the versions of your
source files, following style is recommended. This makes the version
information available as a string, and visible in the
"""My Demo Module. This module is just a demonstration of some common styles.""" __version__ = "$Revision: 1.3 " Note that the module's name will qualift everything created in the
module, it is never necessary to have a prefix in front of each name
inside the module to show its origin. For example, consider a module
that contains classes and functions related to statistical analysis,
called
Poor Names. We don't include extra name prefixes like
Better Names. We would call our internal sample class
This needless over-qualification of names sometimes devolves to
sillines, with class names beginning with Any element of a module with a name that begins with
A common feature of modules is to create a module-wide exception
class. The usuall approach looks like the following. Within a module,
you would define an class Error( Exception ): pass You can then raise your module-specific exception with the following. raise Error, "additional notes" A client module or program can then reference the module's
exception in a
try
statement as
import aModule try: aModule.aFunction() except: aModule.Error, ex: print "problem", ex raise With this style, the origin of the error is shown clearly. |
||