title: how to create singleton class with arguments in python
body: <p>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:</p>
  
  <blockquote>
    <p><strong>Metaclass</strong></p>
  </blockquote>
  
  <pre><code>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
  </code></pre>
  
  <blockquote>
    <p><strong>__new__</strong></p>
  </blockquote>
  
  <pre><code>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
  </code></pre>
  
  <blockquote>
    <p><strong>Decorator</strong></p>
  </blockquote>
  
  <pre><code>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
  </code></pre>
  
  <p>All of them work fine, but when it comes to initiation (like using <strong>__init__</strong> 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:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I don't know if this is the correct way to create singleton that accepts arguments.</p>