Singleton?

Alex Martelli aleax at aleax.it
Sun Jan 27 09:51:49 CET 2002


James T. Dennis wrote:
        ...
>  I'm using a function, rather than an object, so that I can

A function IS an object.  I think you mean "a function rather
than an instance" or "a function rather than a class".  Using
a factory function is the classic Gof4 approach to Singleton.

>  Basically the caller has no reason to know or to care if
>  singleton() is a class (and thus he's invoking a normal

Ability to subclass is the key difference between a factory
function and a class.  The ability to subclass the singleton
class is considered the crucial issue by the Gof4 (and causes
most of the complication, and much of the weakness, in
their section about it).

Why not take a step back and ask -- why do I need to
ensure there is only one instance?  What terrible disasters
would follow if there were several instances, all sharing
the same state (Monostate, Borg, etc)?

Bruce Eckel's "Thinking in Python" draft, downloadable
from his site, is mostly about patterns in Python.


Alex




More information about the Python-list mailing list