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

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

@tshemsedinov tshemsedinov requested a review from agil June 4, 2018 09:38
@tshemsedinov
Copy link
Member

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


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

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

@@ -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

@agil
Copy link
Member

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:
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

@@ -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

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

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:

@tshemsedinov
Copy link
Member

@agil thanks, @AnnaYasenova see review please

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

Successfully merging this pull request may close these issues.

3 participants