Bird
0
0

Why do magic methods in Python usually have names surrounded by double underscores (e.g., __init__, __str__)?

hard📝 Conceptual Q10 of 15
Python - Magic Methods and Operator Overloading
Why do magic methods in Python usually have names surrounded by double underscores (e.g., __init__, __str__)?
ATo avoid name conflicts with user-defined methods
BTo make them private and inaccessible
CTo indicate they are deprecated
DTo mark them as comments
Step-by-Step Solution
Solution:
  1. Step 1: Understand naming convention purpose

    Double underscores signal special methods used internally by Python.
  2. Step 2: Reason about why double underscores are used

    This naming avoids conflicts with normal method names users create.
  3. Final Answer:

    To avoid name conflicts with user-defined methods -> Option A
  4. Quick Check:

    Double underscores prevent naming conflicts [OK]
Quick Trick: Double underscores protect special method names from clashes [OK]
Common Mistakes:
  • Thinking double underscores make methods private
  • Assuming they mark deprecated methods
  • Confusing with comments or docstrings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes