ProgrammingVery hardPython · GATE DA · Advanced
What is printed?
What is printed?
class A:
def __new__(cls, v):
print('new', v)
return super().__new__(B) if v else super().__new__(cls)
def __init__(self, v):
print('init', v)
class B: pass
a = A(1)
b = A(0)
print(type(a).__name__, type(b).__name__)- A. new 1 / new 0 / init 0 / A A
- B. new 1 / new 0 / init 0 / B A
- C. new 1 / init 1 / new 0 / init 0 / B A
- D. new 1 / init 1 / new 0 / init 0 / A A
Correct answer: B. new 1 / new 0 / init 0 / B A
__init__ is called only if __new__ returns an instance of the class (A); the B instance returned for v=1 skips __init__.
More Python Programming questions
- What is printed? python d = {'a': 1, 'b': 2, 'c': 3} del d['a'] d['a'] = 0 d |= {'b': 9, 'z': 4} print(list(d.items()), min(d, key=d.get))
- What is printed? python x = [1, 2, 3, 4, 5, 6] for i in x: if i % 2 == 0: x.remove(i) d = {'a': 1} try: for k in d: d[k + 'x'] = 0 except…
- What is printed? python words = ['bb', 'a', 'cc', 'd', 'aa'] print(sorted(words, key=len, reverse=True)) print(sorted(words, key=lambda w…
- What is printed? python a = list(range(6)) a[1:4] = 'xy' print(a) try: a[::2] = [0, 0] except ValueError as e: print('VE') a[2:2] = [7]…
- What is printed? python a = [1, 2, 3] b = a b += [4] c = a c = c + [5] print(a, c)
- What is printed? python d = {'a': 1, 'b': 2} d['c'] = 3 del d['a'] d['a'] = 9 print(list(d.items()))