ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
class Ordered:
def __init__(self, v): self.v = v
def __lt__(self, o):
print("lt", end=" ")
return self.v < o.v
def __eq__(self, o):
return self.v == o.v
a, b = Ordered(1), Ordered(2)
print(b > a, a <= b if hasattr(Ordered, "__le__") and Ordered.__le__ is not object.__le__ else "no le")- A. True no le
- B. lt True lt True
- C. lt False no le
- D. lt True no le
Correct answer: D. lt True no le
b > a has no __gt__, so Python tries the reflection a.__lt__(b), which prints lt. __le__ is not derived from __lt__ and __eq__ unless functools.total_ordering is applied.
More Python Programming questions
- What is printed? python import functools @functools.totalordering class V: def init(self, n): self.n = n def eq(self, o): return self.n ==…
- What is printed? python def f(): l = [] for i in range(3): try: if i == 0: raise ValueError l.append(i) except ValueError: l.append("v")…
- What is printed? python def risky(n): if n == 1: raise KeyError(n) if n == 2: raise IndexError(n) return n res = [] for n in range(4)…
- What is printed? python class E(Exception): def str(self): return "custom" try: try: raise E("orig") except E as e: raise…
- What is printed? python def f(x): try: return int(x) except ValueError: return float(x) except TypeError: return -1 print(f("3")…
- What is printed? python import math print(math.isclose(0.1 + 0.2, 0.3), math.isclose(1e-10, 0), math.isclose(1e-10, 0, abstol=1e-9)…