A __new__ singleton still runs __init__
QuizMediumPython onlyAsked in: TCS
What does this print?
class Sing:
_inst = None
def __new__(cls, *args):
if cls._inst is None:
cls._inst = super().__new__(cls)
return cls._inst
def __init__(self, name):
print("init", name, end=" ")
self.name = name
s1 = Sing("a")
s2 = Sing("b")
print(s1 is s2, s1.name)init a init b True binit a True ainit a init b False a
This question is part of Pro+
Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.