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")
  1. A. True no le
  2. B. lt True lt True
  3. C. lt False no le
  4. 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

Exams that ask this topic