Difference between a library and a module...

bruno at modulix onurb at xiludom.gro
Tue Mar 7 10:35:54 EST 2006


sophie_newbie wrote:
> OK this might seem like a retarded question,

Better to look like an ignorant than to stay one !-)

> but what is the difference
> between a library and a module?

Python only defines 'modules' and 'packages'. A module can technically
be any python source file, but usually refers to a python source file
that defines symbols (variables, constants, functions, classes...) and
is meant to be imported (vs. a 'script', which is meant to be executed).

A package is a kind of a "super-module" - a collection of modules and
packages -. To make a package, just put your modules in a folder and add
a __init__.py file (which can be empty, the mere existence of this file
is enough to turn your folder into a python package)

'librairy' is a non python-specific, more or less formal term that
refers to a collection of functions, classes, variables etc... (just
like a (real) library is a collection of books).

In Python, 'library' can apply either to an external system lib (.dll on
Windows, .so on *n*x), a collection of packages and modules, a single
package, or even a single module...

> If I do:
> 
> import string
> 
> am I importing a module or a library?

Could be a module, a package, or a module wrapping an external lib
(AFAIK, the string module is a wrapper around a system lib).

The term 'module' refers in fact to two things: the physical python
source file, and the python object created from it by an import
statement. When importing a package, Python creates in the current
namespace a module object[1] from the __init__.py file. Any symbol
defined in the __init__.py will become available as an attribute of the
module object.

Of course, if the __init__.py is empty, this won't give you much !-)

> And if i do string.replace() am I using a module or a function or a
> method or what?

In this case : string.replace() is the function 'replace' defined in the
module 'string'.

More generally: when you import a module (or package FWIW, cf above),
Python creates a module object. Symbols defined in the (physical) module
become attributes of the (object) module. Some of these attributes are
'callable' (functions, classes,... ).

> Sorry.

Why ?

HTH
-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list