Bird
0
0

Why does Python use name mangling for private attributes instead of true access restriction?

hard📝 Conceptual Q10 of 15
Python - Encapsulation and Data Protection
Why does Python use name mangling for private attributes instead of true access restriction?
ATo make private attributes faster to access.
BTo completely prevent any access from outside the class.
CTo enforce strict compile-time access rules.
DTo avoid accidental access but allow intentional access if needed.
Step-by-Step Solution
Solution:
  1. Step 1: Understand Python philosophy

    Python follows a philosophy of "we are all consenting adults," meaning it discourages but does not forbid access to private data.
  2. Step 2: Purpose of name mangling

    Name mangling prevents accidental access by changing attribute names but allows intentional access if the programmer knows the mangled name.
  3. Final Answer:

    To avoid accidental access but allow intentional access if needed. -> Option D
  4. Quick Check:

    Name mangling = soft private, not strict restriction [OK]
Quick Trick: Name mangling prevents accidental, not strict access [OK]
Common Mistakes:
  • Thinking Python enforces strict private access
  • Assuming private attributes are faster
  • Confusing with compile-time restrictions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes