★ wanayoo — archive 1999 https://github.com/HowProgrammingWorks/Singleton/pull/1Nouvelle recherche | Portail wanayoo
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
from

Conversation

@AnnaYasenova
Copy link

@AnnaYasenova AnnaYasenova commented Jun 4, 2018

Implemented singleton in different ways:

  • using inner class
  • using new method
  • using dict
  • inctance of a class variable
  • as class decorator
  • with metaclasses
AnnaYasenova added 2 commits Jun 4, 2018
@tshemsedinov tshemsedinov requested a review from agil Jun 4, 2018
@tshemsedinov
Copy link
Member

@tshemsedinov tshemsedinov commented Jun 4, 2018

🌷 ☭ ☭ ☭ 🌷 thanks for you contribution to the world revolution 🌷 ☭ ☭ ☭ 🌷


class __Singletone:
def __init__(self, arg):

This comment has been minimized.

@agil

agil Jun 4, 2018
Member

invalid formating

def __getattr__(self, name):
return getattr(self.instance, name)


This comment has been minimized.

@agil

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

@@ -0,0 +1,37 @@
class Singletone(object):

This comment has been minimized.

@agil

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

@agil
Copy link
Member

@agil agil commented Jun 4, 2018

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:

This comment has been minimized.

@agil

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
@@ -0,0 +1,19 @@
class Singletone(object):

This comment has been minimized.

@agil

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

Singletone.__instance = object.__new__(cls)
Singletone.__instance.val = val
return Singletone.__instance

This comment has been minimized.

@agil

agil Jun 4, 2018
Member

would be nice to have consistent string representations along all examples

def __str__(self):
    return self.val
pass


MyClass = SingletoneDecorator(MyClass)

This comment has been minimized.

@agil

agil Jun 4, 2018
Member

prefer decorator syntax

@SingletoneDecorator
class MyClass:
@tshemsedinov
Copy link
Member

@tshemsedinov tshemsedinov commented Jun 14, 2018

@agil thanks, @AnnaYasenova see review please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants
You can’t perform that action at this time.