Bird
0
0

What does a single underscore prefix (e.g., _value) in a Python class attribute indicate?

easy📝 Conceptual Q11 of 15
Python - Encapsulation and Data Protection
What does a single underscore prefix (e.g., _value) in a Python class attribute indicate?
AIt is a public attribute accessible everywhere.
BIt is a protected attribute meant for internal use within the class and subclasses.
CIt is a private attribute that cannot be accessed outside the class.
DIt is a special Python keyword for constants.
Step-by-Step Solution
Solution:
  1. Step 1: Understand underscore usage in Python

    A single underscore prefix means the attribute is intended for internal use, signaling protection but not strict privacy.
  2. Step 2: Differentiate from private and public

    Private attributes use double underscores, public have no underscore, and constants are uppercase without underscores.
  3. Final Answer:

    It is a protected attribute meant for internal use within the class and subclasses. -> Option B
  4. Quick Check:

    Single underscore = protected attribute [OK]
Quick Trick: Single underscore means 'protected' by convention [OK]
Common Mistakes:
  • Confusing single underscore with private (double underscore)
  • Thinking single underscore makes attribute inaccessible
  • Assuming single underscore means public attribute

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes