-
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?
Conversation
🌷 ☭ ☭ ☭ 🌷 thanks for you contribution to the world revolution 🌷 ☭ ☭ ☭ 🌷 |
|
||
class __Singletone: | ||
def __init__(self, arg): | ||
|
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
def __getattr__(self, name): | ||
return getattr(self.instance, name) | ||
|
||
|
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.
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
@@ -0,0 +1,37 @@ | |||
class Singletone(object): |
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.
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
would be nice to have consistent tests in all examples, at least print(a is b is c) check |
@@ -0,0 +1,26 @@ | |||
class SingletoneDict: |
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.
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
@@ -0,0 +1,19 @@ | |||
class Singletone(object): |
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.
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
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 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
pass | ||
|
||
|
||
MyClass = SingletoneDecorator(MyClass) |
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.
prefer decorator syntax
@SingletoneDecorator
class MyClass:
@agil thanks, @AnnaYasenova see review please |
Implemented singleton in different ways: