9

I'm looking for the right way to create a singleton class that accepts arguments in the first creation. My research lead me to 3 different ways:

Metaclass

class Singleton(type):
    instance = None
    def __call__(cls, *args, **kwargs):
        if cls.instance is None:
            cls.instance = super(Singleton, cls).__call__(*args, **kwargs)
        return cls.instance

class ASingleton(metaclass=Singleton):
    pass

__new__

class Singleton(object):
    instance = None
    def __new__(cls, *args, **kwargs):
        if cls.instance is None:
            cls.instance = super().__new__(cls, *args, **kwargs)
        return cls.instance

Decorator

def Singleton(myClass):
    instances={}
    def getInstance(*args, **kwargs):
        if myClass not in instances:
            instances[myClass] = myClass(*args, **kwargs)
        return instances[myClass]
    return getInstance

@Singleton
class SingletonTest(object):
    pass

All of them work fine, but when it comes to initiation (like using __init__ in normal class) I can't figure out the right way to implement it. The only solution I can think about is using the metaclass method in this way:

class Singleton(type):
    instance = None

    def __call__(cls, *args, **kwargs):
        if cls.instance is None:
            cls.instance = super(Singleton, cls).__call__(*args, **kwargs)
        return cls.instance

class ASingleton(metaclass=Singleton):
    def __init__(self,j):
        self.j=j

I don't know if this is the correct way to create singleton that accepts arguments.

shadow
  • 647
  • 1
  • 8
  • 18
  • Possible duplicate of [Creating a singleton in Python](https://stackoverflow.com/questions/6760685/creating-a-singleton-in-python) – en_Knight Aug 17 '18 at 14:15
  • that article was my main helpful tutorial, but as u can see, they didn't mention anything about "how to pass arguments or even how to setup the different methods) and that is my question – shadow Aug 17 '18 at 15:52

3 Answers3

8

I've found out that the best way to implement Singleton is by using meta classes:

class Singleton (type):
    _instances = {}
    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls]

# Python 2
class MyClass():
    __metaclass__= Singleton

# Python 3
class MyClass(metaclass=Singleton):
     pass
Andriy Ivaneyko
  • 18,421
  • 4
  • 52
  • 73
  • I second this. I, too, think that metaclasses is the nicest way to implement a Singleton. However, have a look at metaclasses… I found them quite confusing in the beginning. – tahesse Aug 17 '18 at 14:05
  • 1
    @Thomas sure, I cannot argue this ) Meta classes is tricky... But the Singleton is good use of it and is good start for understanding of metaclasses concept – Andriy Ivaneyko Aug 17 '18 at 14:08
  • True, Singleton's in Python actually made me understand Metaclasses :-) – tahesse Aug 17 '18 at 14:09
  • what i understand from metaclass in singleton, it's like re-implement the method __call__() in the class that use "metaclass=singleton" to make sure to create one instance for that class. it make sense if you think in the way that the actual singleton class is the one that use (metaclass=singleton), – shadow Aug 17 '18 at 15:34
3

In addition to @AndriyIvaneyko's answer, here is a thread-safe metaclass singleton implementation:

# Based on tornado.ioloop.IOLoop.instance() approach.
# See https://github.com/facebook/tornado
# Whole idea for this metaclass is taken from: https://stackoverflow.com/a/6798042/2402281
class ThreadSafeSingleton(type):
    _instances = {}
    _singleton_lock = threading.Lock()

    def __call__(cls, *args, **kwargs):
        # double-checked locking pattern (https://en.wikipedia.org/wiki/Double-checked_locking)
        if cls not in cls._instances:
            with cls._singleton_lock:
                if cls not in cls._instances:
                    cls._instances[cls] = super(ThreadSafeSingleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls]

class YourImplementation(metaclass=ThreadSafeSingleton):
    def __init__(self, *args, **kwargs):
        pass  # your implementation goes here

Hope you find it useful!

tahesse
  • 1,351
  • 14
  • 30
  • you mean, this is will make sure to create only one instance by double check the value _instances by making the whole class as critical resource (semaphore approach) – shadow Aug 17 '18 at 15:50
0

I think the solution proposed by @tahesse can raise a dead lock. If the __init__ method contains another singleton than the lock won't be released.

Foe example :

class ThreadSafeSingleton(type):
    _instances = {}
    _singleton_lock = threading.Lock()

    def __call__(cls, *args, **kwargs):
        # double-checked locking pattern (https://en.wikipedia.org/wiki/Double-checked_locking)
        if cls not in cls._instances:
            with cls._singleton_lock:
                if cls not in cls._instances:
                    cls._instances[cls] = super(ThreadSafeSingleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls]

class YourImplementation1(metaclass=ThreadSafeSingleton):
    def __init__(self, *args, **kwargs):
        pass  # your implementation goes here

    def simple_method(self):
        return "this is a test"

class YourImplementation2(metaclass=ThreadSafeSingleton):

    def __init__(self, *args, **kwargs):
        self.your_implementation1 = YourImplementation1()
    def simple_method(self):
        print(self.your_implementation1.simple_method())

So I changed that solution a bit

class ThreadSafeSingleton(type):
    _instances = {}
    _singleton_locks: Dict[Any, threading.Lock] = {}

    def __call__(cls, *args, **kwargs):
        # double-checked locking pattern (https://en.wikipedia.org/wiki/Double-checked_locking)
        if cls not in cls._instances:
            if cls not in cls._singleton_locks:
                cls._singleton_locks[cls] = threading.Lock()
            with cls._singleton_locks[cls]:
                if cls not in cls._instances:
                    cls._instances[cls] = super(ThreadSafeSingleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls]

class YourImplementation1(metaclass=ThreadSafeSingleton):
    def __init__(self, *args, **kwargs):
        pass  # your implementation goes here

    def simple_method(self):
        return "this is a test"

class YourImplementation2(metaclass=ThreadSafeSingleton):

    def __init__(self, *args, **kwargs):
        self.your_implementation1 =YourImplementation1()
    def simple_method(self):
        print(self.your_implementation1.simple_method())
biancama
  • 28
  • 3