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..340c910 --- /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)