Structure of a Python Module

Alternatively, you can view the latest version of this module on the Web at http://code

Like every other NLTK module, begins with a group of comment lines giving a one-line title of the module and identifying the authors. (Since the code is distributed, it also includes the URL where the code is available, a copyright statement, and license information.) Next is the module-level docstring, a triple-quoted multiline string containing information about the module that will be printed when someone types help(nltk.metrics.distance).

# Natural Language Toolkit: Distance Metrics

# Copyright (C) 2001-2009 NLTK Project

# Author: Edward Loper <[email protected]>

# Steven Bird <[email protected]>

# Tom Lippincott <[email protected]>

# URL: <>

# For license information, see LICENSE.TXT

Distance Metrics.

Compute the distance between two items (usually strings). As metrics, they must satisfy the following three requirements:

After this comes all the import statements required for the module, then any global variables, followed by a series of function definitions that make up most of the module. Other modules define "classes," the main building blocks of object-oriented programming, which falls outside the scope of this book. (Most NLTK modules also include a demo() function, which can be used to see examples of the module in use.)

Some module variables and functions are only used within the module. These should have names beginning with an underscore, e.g., _helper(), since this will hide the name. If another module imports this one, using the idiom: from module import *, these names will not be imported. You can optionally list the externally accessible names of a module using a special built-in variable like this:_all_ = ['edit_dis tance', 'jaccard_distance'].

Was this article helpful?

0 0

Post a comment