Why subclass Borg ?

Alex Martelli aleaxit at yahoo.com
Sun Sep 26 00:19:16 CEST 2004

Egbert Bouwman <egbert.list at hccnet.nl> wrote:

> On page 208 in the cookbook Alex seems to say that the Borg class
> should be subclassed. I don't see the necessity.
> Just take the original four-lined Borg(), give it a suitable name,
> maybe add some arguments, add oher methods, and instantiate it.
> Or am I missing something again ?

If you need two classes with "singleton-like behavior", implementing
them as subclasses of Borg is preferable to "copy and paste ``reuse''"
for much the same reason real reuse is always preferable to c&p reuse,
whenever feasible.  Should there ever be any useful maintenance to Borg
(e.g., make it a newstyle class, optimize it in some other way,
whatever!), if you have reused it properly you're in clover; if you have
copied and pasted, you're not.  As usual, there are _no_ compensating
advantages for "copy and paste ``so-called reuse''" whatsoever.

_Once, and ONLY once_.  ANY TIME you have the same functionality coded
twice, in two places, in two copies, that should raise your hackles.
Train yourself to feel DISGUST at such cases, and remove them
immediately whenever you observe them, in favour of a "once and only
once" occurrence of the functionality, reused in two or more places in
the proper ways (function calls, class instantiation, inheritance,
whatever).  You'll be MUCH happier as a result of such a stance,
throughout your programming career.


More information about the Python-list mailing list