Bird
Raised Fist0

Why does Python use name mangling for attributes starting with double underscores inside classes?

hard🧠 Conceptual Q10 of Q15
Python - Encapsulation and Data Protection
Why does Python use name mangling for attributes starting with double underscores inside classes?
ATo make attributes completely private and inaccessible from outside the class.
BTo prevent accidental access and override in subclasses by changing attribute names internally.
CTo optimize memory usage by shortening attribute names.
DTo allow attributes to be accessed globally without restrictions.
Step-by-Step Solution
Solution:
  1. Step 1: Purpose of name mangling

    It changes attribute names internally to avoid accidental name clashes in subclasses.
  2. Step 2: Clarify privacy level

    It does not make attributes fully private; access is still possible with mangled names.
  3. Final Answer:

    To prevent accidental access and override in subclasses by changing attribute names internally. -> Option B
  4. Quick Check:

    Name mangling = avoid subclass conflicts, not full privacy [OK]
Quick Trick: Name mangling prevents accidental overrides, not full privacy [OK]
Common Mistakes:
MISTAKES
  • Thinking name mangling enforces strict privacy
  • Believing it optimizes memory
  • Assuming it allows global access

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes