A simpler singleton pattern?

Gerhard Häring gerhard.haering at gmx.de
Sat Sep 28 21:15:46 EDT 2002


lee lion wrote in comp.lang.python:
> singleton pattern is a way to provide one and only one object of a
> particular type. There are many ways to implement a singleton
> pattern, is the below one?

> class OnlyOne:
>     instance = None
>     def __init__(self, arg):
>         OnlyOne.instance = arg
>     def __getattr__(self, name):
>         return getattr(OnlyOne.instance, name)

That's another cool way. Did you read about the Borg pattern from Alex
Martelli, yet? It works similarly, but without the need to override
__getattr__:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531

-- Gerhard



More information about the Python-list mailing list