Created
April 17, 2020 18:04
-
-
Save victorusachev/51749354494b0c57249fe3cbca3bfd5c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| class SingletonMeta(type): | |
| _instance = None | |
| _args = None | |
| _kwargs = None | |
| def __call__(cls, *args, **kwargs): | |
| if not cls._instance: | |
| cls._args = args | |
| cls._kwargs = kwargs | |
| cls._instance = super(SingletonMeta, cls).__call__(*args, **kwargs) | |
| else: | |
| if cls._args != args or cls._kwargs != kwargs: | |
| raise ValueError | |
| return cls._instance | |
| class Singleton(metaclass=SingletonMeta): | |
| pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment