Bird
0
0

Find the error in this code:

medium📝 Debug Q6 of 15
Python - Magic Methods and Operator Overloading
Find the error in this code:
class Person:
    def __init__(self, age):
        self.age = age
    def __eq__(self, other):
        return self.age = other.age
AAssignment operator used instead of comparison in __eq__
BMissing return statement in __eq__
CIncorrect method name __eq__
DMissing self parameter in __eq__
Step-by-Step Solution
Solution:
  1. Step 1: Check __eq__ method code

    The line return self.age = other.age uses = which is assignment, not comparison.
  2. Step 2: Identify correct operator

    It should use == to compare values, so it should be return self.age == other.age.
  3. Final Answer:

    Assignment operator used instead of comparison in __eq__ -> Option A
  4. Quick Check:

    Use == for comparison, not = [OK]
Quick Trick: Use == for comparison inside __eq__ [OK]
Common Mistakes:
  • Using = instead of ==
  • Forgetting return keyword
  • Wrong method name

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes