How does Mr. Martelli's Borg recipe work ?

John Roth newsgroups at jhrothjr.com
Wed Jul 23 19:05:47 EDT 2003


"Steven Taschuk" <staschuk at telusplanet.net> wrote in message
news:mailman.1058985883.11441.python-list at python.org...
> Quoth John Roth:
>   [...]
> > I can kind of understand the justification for the Borg pattern
> > in Python releases before 2.2, because there was no way of
> > creating a true singleton in those releases. However, in 2.2 and
> > later, it's really easy to create one using new style classes.
>   [...implementing singletons with __new__...]
> > That being the case, I'd like to see the Borg pattern go the way
> > of a fondly remembered hack that is no longer necessary.
>
> Just out of curiosity: why do you prefer singletons to Borgs in
> the first place?

The root of the problem is that someone wants to call the
class constructor and get back something that acts like there's
only one instance. AFIC, the best solution is to not do that; there
are other patterns that are just as easy to understand and a lot
more tractable.

However, assuming that you actually want to do it, 2.2 gives you
a simple and straightforward way to implement the classical
singleton pattern. It's a class where there will be one and only
one instance. However unwise it may be, it's a basic OO
pattern.

> (I don't see Borg as a hack to get the behaviour of a singleton; I
> see it as a more direct way to solve the problem which singletons
> are supposed to solve.  Thus to me Borg is actually preferable, in
> those exceedingly rare cases when that problem actually arises.)

It's a hack because it's fiddling with the internal representation
of the object. At one time I had to maintain a large set of
changes to IBM's MVS, and I'm very sensitive to what happens
to the developer's flexibility when someone nails an internal
issue that ***SHOULD*** be subject to change between
releases without consequences, and suddenly forecloses
the developers options in the area.

Bad. Bad. Bad.

John Roth
>
> -- 
> Steven Taschuk                             staschuk at telusplanet.net
> "I may be wrong but I'm positive."  -- _Friday_, Robert A. Heinlein
>






More information about the Python-list mailing list