Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Singleton in python #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Python/1-inner_class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Singletone:

class __Singletone:
def __init__(self, arg):

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

invalid formating

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)


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

following print statements do not verify that a, b, c are singletones:
a, b and c have different addresses and they are different objects
only a.instance b.instance and c.instance is a singleton

a = Singletone('first')
print(a)
b = Singletone('second')
print(b)
c = Singletone('third')
print('a: ', a)
print('b: ', b)
print('c: ', c)
37 changes: 37 additions & 0 deletions Python/2-new_method.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Singletone(object):
Copy link
Member

@agil agil Jun 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inconsistent class declaration, redundant object inheritance for python 3 or missing object as superclass in other places for python2. But who uses python2 in 2018.
use copy-pasta smarter


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)
26 changes: 26 additions & 0 deletions Python/3-dict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class SingletoneDict:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class SingletoneDict is redundant
_state_dict can be stored directly in Singletone class

class Singletone:
    _state_dict = {}

    def __init__(self, arg):
        self.__dict__ = self._state_dict
        self.val = arg

    def __str__(self):
        return self.val


_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)
19 changes: 19 additions & 0 deletions Python/4-class_variable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Singletone(object):
Copy link
Member

@agil agil Jun 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inconsistent class declaration, redundant object inheritance for python 3 or missing object as superclass in other places for python2. But who uses python2 in 2018.
use copy-pasta wiser


__instance = None

def __new__(cls, val):
if Singletone.__instance is None:
Singletone.__instance = object.__new__(cls)
Singletone.__instance.val = val
return Singletone.__instance

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice to have consistent string representations along all examples

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)
30 changes: 30 additions & 0 deletions Python/5-decorator.py
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer decorator syntax

@SingletoneDecorator
class 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)
35 changes: 35 additions & 0 deletions Python/6-metaclass.py
Original file line number Diff line number Diff line change
@@ -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)