Return to Snippet

Revision: 3082
at June 1, 2007 20:24 by n3x


Updated Code
class Singleton(type):
    def __init__(self, *args, **kwds):
        type.__init__(self, *args, **kwds)
        self._instances = {}

    def __call__(self, *args, **kwds):
        sig = args + tuple(sorted(kwds.items()))
        if not sig in self._instances:
            self._instances[sig] = type.__call__(self, *args, **kwds)
        return self._instances[sig]

Revision: 3081
at June 1, 2007 20:20 by n3x


Updated Code
class Singleton(type):
    def __init__(self, *args, **kwds):
        type.__init__(self, *args, **kwds)
        self._instances = {}

    def __call__(self, *args, **kwds):
        sig = args + tuple(sorted(kwds.values()))
        if not sig in self._instances:
            self._instances[sig] = type.__call__(self, *args, **kwds)
        return self._instances[sig]

Revision: 3080
at June 1, 2007 20:11 by n3x


Updated Code
class Singleton(type):
    def __init__(self, *args, **kwds):
        type.__init__(self, *args, **kwds)
        self._instances = {}

    def __call__(self, *args, **kwds):
        sig = args + tuple(dictToList(kwds))
        if not sig in self._instances:
            self._instances[sig] = type.__call__(self, *args, **kwds)
        return self._instances[sig]

def dictToList(dict):
    """ dictionaries have the values() method, but two equivalent dicts may
        return differently ordered lists. this function sorts by dictionary
        keys in order to eliminate that problem.
    """
    keys = dict.keys()
    keys.sort()
    values = [dict[k] for k in keys]
    return zip(keys, values)

Revision: 3079
at June 1, 2007 20:07 by n3x


Initial Code
class Singleton(type):
    def __init__(self, *args, **kwds):
        type.__init__(self, *args, **kwds)
        self._instances = {}

    def __call__(self, *args, **kwds):
        if len(kwds) > 0:
            sig = args + tuple(dictToList(kwds))
        else:
            sig = args
        if not sig in self._instances:
            self._instances[sig] = type.__call__(self, *args, **kwds)
        return self._instances[sig]

def dictToList(dict):
    """ dictionaries have the values() method, but two equivalent dicts may
        return differently ordered lists. this function sorts by dictionary
        keys in order to eliminate that problem.
    """
    keys = dict.keys()
    keys.sort()
    values = [dict[k] for k in keys]
    return zip(keys, values)

Initial URL

                                

Initial Description
The ASPN cookbook has many recipes for singletons in Python. So far, this one
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/412551
has been my favourite, because it is so simple and concise. However, I just ran into a brick wall when I tried to use it with a class that can be initialized with keyword arguments. This is my first draft for a solution to this problem. It's quick and dirty; please give it a glance and leave a comment if you find a problem.

Initial Title
Making Daniel Brodie's singleton recipe work with keyword arguments

Initial Tags
python

Initial Language
Python