From 2af7ebce18123b633430ffe7cbe32618ee0bb5fc Mon Sep 17 00:00:00 2001 From: Anna Yasenova Date: Mon, 4 Jun 2018 09:01:35 +0300 Subject: [PATCH 1/2] Implemented singletone in python. --- Python/1-inner_class.py | 29 +++++++++++++++++++++++++++++ Python/2-new_method.py | 37 +++++++++++++++++++++++++++++++++++++ Python/3-dict.py | 26 ++++++++++++++++++++++++++ Python/4-class_variable.py | 19 +++++++++++++++++++ Python/5-decorator.py | 30 ++++++++++++++++++++++++++++++ Python/6-metaclass.py | 35 +++++++++++++++++++++++++++++++++++ 6 files changed, 176 insertions(+) create mode 100644 Python/1-inner_class.py create mode 100644 Python/2-new_method.py create mode 100644 Python/3-dict.py create mode 100644 Python/4-class_variable.py create mode 100644 Python/5-decorator.py create mode 100644 Python/6-metaclass.py diff --git a/Python/1-inner_class.py b/Python/1-inner_class.py new file mode 100644 index 0000000..d06d675 --- /dev/null +++ b/Python/1-inner_class.py @@ -0,0 +1,29 @@ +class Singletone: + + class __Singletone: + def __init__(self, arg): + + self.val = arg + def __str__(self): + return repr(self) + self.val + + instance = None + + def __init__(self, arg): + if not Singletone.instance: + Singletone.instance = Singletone.__Singletone(arg) + else: + Singletone.instance.val = arg + + def __getattr__(self, name): + return getattr(self.instance, name) + + +a = Singletone('first') +print(a) +b = Singletone('second') +print(b) +c = Singletone('third') +print('a: ', a) +print('b: ', b) +print('c: ', c) diff --git a/Python/2-new_method.py b/Python/2-new_method.py new file mode 100644 index 0000000..82283eb --- /dev/null +++ b/Python/2-new_method.py @@ -0,0 +1,37 @@ +class Singletone(object): + + class __Singletone: + def __init__(self): + self.val = None + + def __str__(self): + return repr(self) + self.val + + instance = None + + def __new__(cls): + if not Singletone.instance: + Singletone.instance = Singletone.__Singletone() + return Singletone.instance + + def __getattr__(self, name): + return getattr(self.instance, name) + + def __setattr__(self, name): + return setattr(self.instance, name) + + +a = Singletone() +a.val = 'first' +print(a) + +b = Singletone() +b.val = 'second' +print(b) + +c = Singletone() +c.val = 'third' + +print('a: ', a) +print('b: ', b) +print('c: ', c) diff --git a/Python/3-dict.py b/Python/3-dict.py new file mode 100644 index 0000000..77bf255 --- /dev/null +++ b/Python/3-dict.py @@ -0,0 +1,26 @@ +class SingletoneDict: + + _state_dict = {} + + def __init__(self): + self.__dict__ = self._state_dict + + +class Singletone(SingletoneDict): + + def __init__(self, arg): + SingletoneDict.__init__(self) + self.val = arg + + def __str__(self): + return self.val + + +a = Singletone('first') +print(a) +b = Singletone('second') +print(b) +c = Singletone('third') +print('a: ', a) +print('b: ', b) +print('c: ', c) diff --git a/Python/4-class_variable.py b/Python/4-class_variable.py new file mode 100644 index 0000000..f652f8a --- /dev/null +++ b/Python/4-class_variable.py @@ -0,0 +1,19 @@ +class Singletone(object): + + __instance = None + + def __new__(cls, val): + if Singletone.__instance is None: + Singletone.__instance = object.__new__(cls) + Singletone.__instance.val = val + return Singletone.__instance + + +a = Singletone('first') +print(a) +b = Singletone('second') +print(b) +c = Singletone('third') +print('a: ', a) +print('b: ', b) +print('c: ', c) diff --git a/Python/5-decorator.py b/Python/5-decorator.py new file mode 100644 index 0000000..8624ead --- /dev/null +++ b/Python/5-decorator.py @@ -0,0 +1,30 @@ +class SingletoneDecorator: + + def __init__(self, arg_class): + self.arg_class = arg_class + self.instance = None + + def __call__(self, *args, **kwargs): + if self.instance == None: + self.instance = self.arg_class(*args, **kwargs) + return self.instance + +class MyClass: + pass + + +MyClass = SingletoneDecorator(MyClass) + +a = MyClass() +b = MyClass() +c = MyClass() + +a.val = 'first' +b.val = 'second' +c.val = 'third' + +print(a.val) +print(b.val) +print(c.val) + +print(a is b is c) diff --git a/Python/6-metaclass.py b/Python/6-metaclass.py new file mode 100644 index 0000000..99cf401 --- /dev/null +++ b/Python/6-metaclass.py @@ -0,0 +1,35 @@ +class Metaclass(type): + + def __init__(cls, name, bases, dict): + + super(Metaclass, cls).__init__(name, bases, dict) + + new_origin = cls.__new__ + + def new_custom(cls, *args, **kwargs): + if cls.instance == None: + cls.instance = new_origin(cls, *args, **kwargs) + return cls.instance + + cls.instance = None + cls.__new__ = staticmethod(new_custom) + +class MyClass(object): + + __metaclass__ = Metaclass + + def __init__(self,val): + self.val = val + + def __str__(self): + return repr(self) + self.val + +a = MyClass('first') +b = MyClass('second') +c = MyClass('third') + +print('a: ', a) +print('b: ', b) +print('c: ', c) + +print(a is b is c) \ No newline at end of file From 2bb9aa60cf8dfff1faa48ebfa47ed40ce7f54f1d Mon Sep 17 00:00:00 2001 From: AnnaYasenova Date: Mon, 4 Jun 2018 09:03:27 +0300 Subject: [PATCH 2/2] Update 6-metaclass.py --- Python/6-metaclass.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/6-metaclass.py b/Python/6-metaclass.py index 99cf401..340c910 100644 --- a/Python/6-metaclass.py +++ b/Python/6-metaclass.py @@ -32,4 +32,4 @@ def __str__(self): print('b: ', b) print('c: ', c) -print(a is b is c) \ No newline at end of file +print(a is b is c) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy