[Tutor] What should go in a module?

Alan Gauld alan.gauld at btinternet.com
Thu Feb 8 01:06:11 CET 2007

"Don Taylor" <nospamformeSVP at gmail.com> wrote

>I am looking for advice on module size.

There is no absolute answer to this.

> When I first came upon Python my initial thought was to package very
> closely-related things into modules. Modules would be small, and may
> contain a single class or function.  An application would consist of
> lots of modules.

Thats probably not the best approachj.
Remember that a module is basically a unit of reuse.
If you can't use a module simply by importting it then
its too small.

> package everything together into a single module and use separate
> modules only when that code might usefully be shared between 
> different
> applications.

Thats the critical factor. Split it up into reusable chunks.
If you have some functions and classes that together can be used
by another application make them into a module. If the module
can only be used by also importing some other module then keep
the two modules together as a single unit.

In other words modules should be as small as possible but
no smaller...

> I am really just asking about applications here, not libraries or
> frameworks.

The same rules apply. A module is a library in Python terms.


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list