IMAGINATION IN PROGRESS

Skip to content

Move to Singleton for Configuration Class

There's a whole bunch of messy boilerplate around the use of the configuration class. It seems that would be easier to deal with if the configuration were, in fact, a singleton and could only ever be defined once.

class Singleton(object):
    _instance = None
    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super(Singleton, cls).__new__(
                                cls, *args, **kwargs)
        return cls._instance


if __name__ == '__main__':
    s1 = Singleton()
    s2 = Singleton()
    if (id(s1) == id(s2)):
        print "Same"
    else:
        print "Different"

Reference: https://stackoverflow.com/a/1810367/10406011