Singleton via __new__

Hard ⏱ 18 min 25% acceptance ★★★★☆ 4.3
Implement AppConfig so that every construction returns the same instance by overriding __new__ to cache the first instance on the class. AppConfig() is AppConfig() must be True. Mention in discussion why module-level instances are usually the more Pythonic singleton.

Examples

Example 1
Input
AppConfig() is AppConfig()
Output
True
Explanation

__new__ returned the cached object the second time.

Constraints

  • Override __new__, not __init__.
  • Cache on a class attribute.

Topics

OOP__new__singleton

Companies

OracleSAPVMware

Hints

Hint 1

cls._instance = super().__new__(cls) on first call.

Hint 2

Return the cached one afterwards.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.