singletons

Asun Friere afriere at yahoo.co.uk
Wed Jul 16 23:05:47 EDT 2008


On Jul 17, 8:20 am, Craig Allen <callen... at gmail.com> wrote:

> Is it pythonic?

You probably can't get anymore pythonic than something written by the
BDFL.  In describing the use of __new__ in <i>Unifying types and
classes in Python 2.2</i> he gives this recipe for a Singleton.


class Singleton(object):
    def __new__(cls, *args, **kwds):
        it = cls.__dict__.get("__it__")
        if it is not None:
            return it
        cls.__it__ = it = object.__new__(cls)
        it.init(*args, **kwds)
        return it
    def init(self, *args, **kwds):
        pass

You might find this a useful starting point.



More information about the Python-list mailing list