← Quiz: Design Patterns: all 10 questions
Quiz: Design Patterns · Question 2 of 10
What does this print? (code)
What does this print?
class Logger:
_one = None
def __new__(cls):
if cls._one is None:
cls._one = super().__new__(cls)
cls._one.lines = []
return cls._one
Logger().lines.append('start')
Logger().lines.append('stop')
print(len(Logger().lines))
- A.0
- B.1
- C.2
- D.3
Answer & explanation
Correct answer: 2
Every Logger() returns the same object, so both appends go to one list.
Can you get the rest right without peeking?
Take the timed Quiz: Design Patterns quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →