HardPro challengePython

Metaclass — Singleton Pattern

PythonMetaprogrammingOOP

Implement a SingletonMeta metaclass so that any class using it as its metaclass can only ever have one instance.

class MyService(metaclass=SingletonMeta):
    pass

a = MyService()
b = MyService()
assert a is b  # same object

Implement solve() which:
1. Creates two instances of MyService.
2. Returns True if they are the same object (is), False otherwise.

Constraints

  • Implement the singleton logic inside SingletonMeta.__call__.
  • Do not modify MyService itself.

Sample tests

Test #1Two instantiations return the same object
Input: []
Output: true
Test #2Second call also returns the cached instance
Input: []
Output: true
Test #3Third call still returns the same instance
Input: []
Output: true