Gang of forfeit

Thomas Ptacek | May 19th, 2005 | Filed Under: Uncategorized

In my continuing series on being baffled by the popularity of Python (and grudgingly proceeding to select it for every new project I start), I will now pick a nit in “Design Patterns in Python”.

This code:

class Singleton: __single = None def __init__( self ): if Singleton.__single: raise Singleton.__single Singleton.__single = self

… misses the point of the Singleton pattern. The idea behind Singleton is to make it difficult to accidentally create two of the same Singletons, not to punish you for an accident that is still easy to make.

So far the best Singleton imp I can find in the mailing lists is:

class _Singleton: ... Singleton = _Singleton()

See how the “real” class has an underscore? See how that means nobody will call it directly? See? Sigh.

No comments yet. Be the first.

Leave a reply