Skip to content

Instantly share code, notes, and snippets.

@ixuuux
Last active April 19, 2023 03:46
Show Gist options
  • Save ixuuux/64af45ff6676ca3e1bbc15d84a44ce15 to your computer and use it in GitHub Desktop.
Save ixuuux/64af45ff6676ca3e1bbc15d84a44ce15 to your computer and use it in GitHub Desktop.
一些小工具
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