title: Creating Singleton Metaclass in Python that can be extended to multiple derived classes
body: <p>I recently came across Python's metaclass concept and I am trying to enforce <code>Singleton</code> for a few existing classes. I want to reuse the previously created instance when the class is instantiated again.</p>
  <p>Here is my attempt at it. But the singleton condition doesn't seem to get enforced.</p>
  <p>The existing class that should enforce <code>Singleton</code>. I have multiple such classes like Bike, Train which inherits <code>Vehicle</code> and all these should be made singleton.</p>
  <pre><code>from singleton import Singleton
  from vehicle import Vehicle
  
  class Car(Vehicle, object):
      __metaclass__ = Singleton
  
      def __init__(self):
          print(&quot;Constructor: Car is starting&quot;)
          pass
  
      def print_car(self):
          print(&quot;Car is running&quot;)
  </code></pre>
  <p>Car class inherits a <code>Vehicle</code></p>
  <pre><code>from abc import ABC
  
  class Vehicle(ABC):
      def __init__(self):
          pass
  </code></pre>
  <p>Here is my attempt at <code>Singleton</code>. I used <a href="https://stackoverflow.com/questions/6760685/creating-a-singleton-in-python">a few</a> reference <a href="https://stackoverflow.com/questions/31875/is-there-a-simple-elegant-way-to-define-singletons">SO posts</a>.</p>
  <pre><code>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]
  </code></pre>
  <p>However, when I create multiple objects for <code>Car</code>, I don't see singleton being enforced.</p>
  <pre><code>car = Car()
  car.print_car()
  car2 = Car()
  car2.print_car()
  print(car is car2)  # Returns False
  </code></pre>
  <p>As I am still learning Python's metaclass and how it works, wondering what I am missing here?</p>
  <p>All these classes (base class and derived class) are in my control and I can make changes to them. How can I achieve enforcing singleton to my sub-classes like <code>Car</code>, <code>Bike</code> etc?</p>
  <p>[Update 1]
  Using Python 3 version of metaclass gives me the below error</p>
  <pre><code>from singleton import Singleton
  from vehicle import Vehicle
  
  class Car(Vehicle, object, metaclass=Singleton):
  # class Car(Vehicle, object):
      # __metaclass__ = Singleton
      def __init__(self):
          print(&quot;Constructor: Car is starting&quot;)
  
      def print_car(self):
          print(&quot;Car is running&quot;)
  </code></pre>
  <p>Error:</p>
  <pre><code>  File &quot;/Users/user/HelloVsCode/hello.py&quot;, line 5, in &lt;module&gt;
      from car import Car
    File &quot;/Users/user/HelloVsCode/car.py&quot;, line 7, in &lt;module&gt;
      class Car(Vehicle, object, metaclass=Singleton):
  TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
  </code></pre>