What Is A Module? - Page 2
May 3, 2001
Modules are used to organize larger Python projects. The Python
language itself is split into modules to make it more manageable.
You don't need to organize your own code into modules, but if
you're writing any programs more than a few pages long, or any
code that you want to reuse, you should probably do so.
A module is a file containing code. A module defines a group of
Python functions or other objects. The name of the module is
derived from the name of the file. Modules will most often
contain Python source code, but they can also be compiled C or
C++ object files. Compiled modules and Python source modules are
used in the same way.
A module is just a single file containing related functions,
constants, and so forth.
As well as grouping related Python objects, modules help avoid
name clash problems. For example, you might write a module for
your program called MyModule, which defines a
function called reverse. In the same program you
might also wish to make use of somebody else's module called
OtherModule, which also defines a function called
reverse, but which does something different from your
reverse function. In a language without modules, it
would be impossible to use two different reverse functions. In
Python, it's trivial — you simply refer to them in your main
program as MyModule.reverse and OtherModule.reverse.
Modules are also used to make Python itself more manageable. Most
standard Python functions are not built into the core of the
language, but instead are provided via specific modules, which
the programmer can load as needed.
The Quick Python Book
The Quick Python Book
A First Module - Page 3
|