title: How to write an inheritable Singleton in Python for classes that take arguments upon init?
body: <p>Following <a href="https://stackoverflow.com/a/8237462/913098">this</a>, I wrote the following</p>
  <pre><code>class Singleton(object):
      &quot;&quot;&quot;
      Inherit this to ensure a single instance of the inheriting class if ever created.
      This also affects classes that inherit the inheriting class, recursively.
      &quot;&quot;&quot;
      _instances = {}
  
      def __new__(cls, *args, **kwargs):
          if cls._instances.get(cls, None) is None:
              cls._instances[cls] = super(Singleton, cls).__new__(cls, *args, **kwargs)
  
          return Singleton._instances[cls]
  
  
  class A(Singleton):
      def __init__(self, value):
          self.value = value
  
  
  class B(A):
      def __init__(self, value):
          super().__init__(value)
          self.value = value
  
  
  a1 = A(1)
  a2 = A(2)
  b1 = B(3)
  b2 = B(4)
  
  
  print(f&quot;1: {a1.value} 2: {a2.value} 3: {b1.value} 4: {b2.value}&quot;)
  
  </code></pre>
  <p>Which fails with</p>
  <pre><code>Traceback (most recent call last):
    File &quot;/home/noam.s/src/uv_metadata_thin/uv_metadata_thin/factories/sdfsdf.py&quot;, line 15, in &lt;module&gt;
      a1 = A(1)
    File &quot;/home/noam.s/src/uv_metadata_thin/uv_metadata_thin/utils/singleton.py&quot;, line 10, in __new__
      cls._instances[cls] = super(Singleton, cls).__new__(cls, *args, **kwargs)
  TypeError: object.__new__() takes exactly one argument (the type to instantiate)
  </code></pre>
  <p>I expected only the 1st parameters to &quot;catch&quot;, meaning output should be</p>
  <p><code>&quot;1: 1 2: 1 3: 3 4: 3&quot;</code></p>
  <p>and certainly not fail.</p>
  <hr />
  <p>How to write an inheritable Singleton whose inheriting classes may define an <code>__init__</code> that takes in arguments?</p>