Bird
0
0

Which of the following is the correct syntax to define the less than operator < in a Python class?

easy📝 Syntax Q12 of 15
Python - Magic Methods and Operator Overloading
Which of the following is the correct syntax to define the less than operator < in a Python class?
Adef __lt__(self):
Bdef __less_than__(self, other):
Cdef __less__(self, other):
Ddef __lt__(self, other):
Step-by-Step Solution
Solution:
  1. Step 1: Recall the magic method name for <

    The method for < is __lt__ and it takes two parameters: self and other.
  2. Step 2: Check method signature correctness

    Correct syntax is def __lt__(self, other):. Other options have wrong names or missing parameters.
  3. Final Answer:

    def __lt__(self, other): -> Option D
  4. Quick Check:

    Less than operator uses __lt__(self, other) [OK]
Quick Trick: Magic methods for comparisons always take self and other [OK]
Common Mistakes:
  • Using wrong method names like __less_than__
  • Omitting the other parameter
  • Using incorrect method signatures

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes