[Tutor] Python structure advice ?

Alan Gauld alan.gauld at freenet.co.uk
Sat Dec 18 00:31:41 CET 2004

> For what it's worth, it seems to me to be perfectly normal to have
> classes that are only ever intended to have a single instance.  For
> example, you're never likely to need more than one HTML parser, and
> yet htmllib.HTMLParser is a class...

That's true but the argument for a class in that case is that we
can subclass it for more specialized purposes. If there is only
to be a single instance and it will not be specialized by sub
classing then a simple module will do the job just nicely.

> As Kent said, the main point of a class is that you have a
> of data and operations on that data bundled together.

Dunno if I'd agree that that was the *main point* of classes,
the main point I'd say was to act as a template for objects.
The fact that there might only be one instance is a side issue.

But creating classes that only have a single instance is certainly
OK, after all the original design patterns book by the GoF has a
singleton pattern to ensure that only one oinstance can be created!

> "I want lots of things like this", as it is a declaration of
> modularity -- "This stuff all belongs together as a unit".

So use a module... Python is blessed with both constructs and
we should use whichever is most appropriate. IMHO of course! :-)

Alan G
Author of the Learn to Program web tutor

More information about the Tutor mailing list