Last active
April 19, 2023 03:46
-
-
Save ixuuux/64af45ff6676ca3e1bbc15d84a44ce15 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 Singleton(object): | |
""" 单例类 | |
> @Singleton | |
> class Demo: | |
> def __init__(self): | |
> pass | |
> | |
> assert Demo() == Demo() | |
""" | |
def __init__(self, cls): | |
self._cls = cls | |
self._instance = {} | |
def __call__(self, *args, **kwargs): | |
if self._cls not in self._instance: | |
self._instance[self._cls] = self._cls(*args, **kwargs) | |
return self._instance[self._cls] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment