Created
June 14, 2015 11:21
-
-
Save c7h/91528dd8069d33d7ea25 to your computer and use it in GitHub Desktop.
the _drop-method is used for unittesting. Easy way to get rid of all your old instances.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'Christoph Gerneth' | |
class SingletonType(type): | |
def __call__(self, *args, **kwargs): | |
try: | |
return self.__instance | |
except AttributeError: | |
self.__instance = super(SingletonType, self).__call__(*args, **kwargs) | |
return self.__instance | |
def _drop(self): | |
"Drop the instance (for testing purposes)." | |
del self.__instance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to use the singleton instance:
and how to use them in unittests: