Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make utils.py a singleton class. #6

Open
vixadd opened this issue Jun 2, 2017 · 3 comments
Open

Make utils.py a singleton class. #6

vixadd opened this issue Jun 2, 2017 · 3 comments

Comments

@vixadd
Copy link

vixadd commented Jun 2, 2017

Here's my own implementation of singletons. All you have to do is decorate the class; to get the singleton, you then have to use the Instance method. Here's an example:

@Singleton
class Foo:
   def __init__(self):
       print 'Foo created'

f = Foo() # Error, this isn't how you get the instance of a singleton

f = Foo.Instance() # Good. Being explicit is in line with the Python Zen
g = Foo.Instance() # Returns already created instance

print f is g # True

And here's the code:

class Singleton:
    """
    A non-thread-safe helper class to ease implementing singletons.
    This should be used as a decorator -- not a metaclass -- to the
    class that should be a singleton.

    The decorated class can define one `__init__` function that
    takes only the `self` argument. Also, the decorated class cannot be
    inherited from. Other than that, there are no restrictions that apply
    to the decorated class.

    To get the singleton instance, use the `Instance` method. Trying
    to use `__call__` will result in a `TypeError` being raised.

    """

    def __init__(self, decorated):
        self._decorated = decorated

    def Instance(self):
        """
        Returns the singleton instance. Upon its first call, it creates a
        new instance of the decorated class and calls its `__init__` method.
        On all subsequent calls, the already created instance is returned.

        """
        try:
            return self._instance
        except AttributeError:
            self._instance = self._decorated()
            return self._instance

    def __call__(self):
        raise TypeError('Singletons must be accessed through `Instance()`.')

    def __instancecheck__(self, inst):
        return isinstance(inst, self._decorated)
@vixadd
Copy link
Author

vixadd commented Jun 2, 2017

For python in general it is suggested that there be a single module to handle singleton operations. However, I nixed this because of the way that utils operates.

@Cameron-Calpin
Copy link
Collaborator

So the singleton class above in the example is what we're implementing in the Utils class to check if its an instance, correct? Sorry, I just need a bit of clarification

@vixadd
Copy link
Author

vixadd commented Jun 2, 2017

Yeah, so the way a singleton works is that it creates a single object when it's first referenced, and all subsequent module that reference it, reference that object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants