CS FundamentalsVery hardProduct-company placement · Python interview
This Singleton caches its instance in new. What does it print?
This Singleton caches its instance in __new__. What does it print?
class Settings:
_instance = None
def __new__(cls, *args):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
def __init__(self, env):
self.env = env
x = Settings("prod")
y = Settings("test")
print(x is y, x.env)- A. True prod
- B. False prod
- C. True test
- D. False test
Correct answer: C. True test
__new__ returns the same object, but Python still calls __init__ on it at every construction, overwriting env with 'test'. So x is y, and x.env has silently changed - a common bug in __new__-based singletons.
Practise more Software Engineering, Git and Cloud Basics questionsSoftware Engineering, Git and Cloud Basics: notes and key points
More Software Engineering, Git and Cloud Basics questions
- Module report.py does from pricing import getrate and calls getrate() inside build(). A unit test for build() uses…
- A faulty merge M of feature into main was undone with git revert -m 1 M. The feature team later fixes the bug with a new commit on feature…
- You run git stash pop and it reports a merge conflict in f.txt. What happens to the stash entry?
- During git rebase -i, what is the difference between marking a commit 'squash' and marking it 'fixup'?
- On main you need the version of config.yml that exists on branch release, without merging anything else. Which command does this, and what…
- A file was renamed from util.js to helpers.js three months ago. git log helpers.js shows only the commits since the rename. Which command…