Bird
0
0

Given a class Secret with a name-mangled attribute __key, how can you access __key from outside the class?

easy📝 Syntax Q3 of 15
Python - Encapsulation and Data Protection
Given a class Secret with a name-mangled attribute __key, how can you access __key from outside the class?
AUsing <code>instance._Secret__key</code>
BUsing <code>instance.__key</code>
CUsing <code>instance._key</code>
DUsing <code>instance.key</code>
Step-by-Step Solution
Solution:
  1. Step 1: Recall name mangling format

    Attributes starting with double underscores are renamed to _ClassName__attr.
  2. Step 2: Access mangled attribute

    Access __key as _Secret__key from outside.
  3. Final Answer:

    Using instance._Secret__key -> Option A
  4. Quick Check:

    Access mangled names with _ClassName prefix [OK]
Quick Trick: Access with _ClassName__attr outside class [OK]
Common Mistakes:
  • Trying to access __key directly
  • Using single underscore instead
  • Assuming attribute is public

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes