Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
Singleton in python #1
Conversation
|
|
|
|
||
| class __Singletone: | ||
| def __init__(self, arg): | ||
|
|
agil
Jun 4, 2018
Member
invalid formating
invalid formating
| def __getattr__(self, name): | ||
| return getattr(self.instance, name) | ||
|
|
||
|
|
agil
Jun 4, 2018
Member
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
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): | |||
agil
Jun 4, 2018
•
Member
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
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: | |||
agil
Jun 4, 2018
Member
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
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): | |||
agil
Jun 4, 2018
•
Member
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
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 | ||
|
|
agil
Jun 4, 2018
Member
would be nice to have consistent string representations along all examples
def __str__(self):
return self.val
would be nice to have consistent string representations along all examples
def __str__(self):
return self.val| pass | ||
|
|
||
|
|
||
| MyClass = SingletoneDecorator(MyClass) |
agil
Jun 4, 2018
Member
prefer decorator syntax
@SingletoneDecorator
class MyClass:
prefer decorator syntax
@SingletoneDecorator
class MyClass:|
@agil thanks, @AnnaYasenova see review please |
Implemented singleton in different ways: