From 186715ff05d20b472faa841c8e98c08f927e55ad Mon Sep 17 00:00:00 2001 From: omeedforoughi Date: Thu, 1 Oct 2020 17:56:54 +0330 Subject: [PATCH 1/2] =?UTF-8?q?=D8=A7=D9=81=D8=B2=D9=88=D8=AF=D9=86=20?= =?UTF-8?q?=D9=85=D8=AB=D8=A7=D9=84=20=D8=B3=DB=8C=D9=86=DA=AF=D9=84=D8=AA?= =?UTF-8?q?=D9=88=D9=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../725.singleton/740.code_example.py | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 sessions/700.design-patterns-in-depth/725.singleton/740.code_example.py diff --git a/sessions/700.design-patterns-in-depth/725.singleton/740.code_example.py b/sessions/700.design-patterns-in-depth/725.singleton/740.code_example.py new file mode 100644 index 0000000..a9efd8d --- /dev/null +++ b/sessions/700.design-patterns-in-depth/725.singleton/740.code_example.py @@ -0,0 +1,24 @@ +class SingletonMeta(type): + + _instance = None + def __call__(self, *args, **kwargs): + if self._instance is None: + self._instance = super().__call__() + return self._instance + +class Singleton(metaclass=SingletonMeta): + + def some_logic(self): + pass + + + +if __name__ == "__main__": + + s1 = Singleton() + s2 = Singleton() + + if id(s1) == id(s2): + print('Singleton works') + else: + print('singleton failed') From 6c0525fee4724a61eacb4be56f5543dde06f0ff5 Mon Sep 17 00:00:00 2001 From: Omeed foroughi Date: Thu, 1 Oct 2020 22:16:09 +0330 Subject: [PATCH 2/2] =?UTF-8?q?=D8=AA=D8=BA=DB=8C=DB=8C=D8=B1=D8=A7=D8=AA?= =?UTF-8?q?=20=D9=85=D8=AB=D8=A7=D9=84=20=D8=B3=DB=8C=D9=86=DA=AF=D9=84?= =?UTF-8?q?=D8=AA=D9=88=D9=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../725.singleton/740.code_example.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sessions/700.design-patterns-in-depth/725.singleton/740.code_example.py b/sessions/700.design-patterns-in-depth/725.singleton/740.code_example.py index a9efd8d..4622f4e 100644 --- a/sessions/700.design-patterns-in-depth/725.singleton/740.code_example.py +++ b/sessions/700.design-patterns-in-depth/725.singleton/740.code_example.py @@ -22,3 +22,4 @@ def some_logic(self): print('Singleton works') else: print('singleton failed') +