-
Notifications
You must be signed in to change notification settings - Fork 8
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. following |
||
a = Singletone('first') | ||
print(a) | ||
b = Singletone('second') | ||
print(b) | ||
c = Singletone('third') | ||
print('a: ', a) | ||
print('b: ', b) | ||
print('c: ', c) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
class Singletone(object): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. inconsistent class declaration, redundant object inheritance for python 3 or missing |
||
|
||
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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class SingletoneDict: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Singletone(object): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. inconsistent class declaration, redundant object inheritance for python 3 or missing |
||
|
||
__instance = None | ||
|
||
def __new__(cls, val): | ||
if Singletone.__instance is None: | ||
Singletone.__instance = object.__new__(cls) | ||
Singletone.__instance.val = val | ||
return Singletone.__instance | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
invalid formating