Bird
Raised Fist0

What is the cause of the error in this code?

medium📝 Debug Q6 of Q15
Python - Encapsulation and Data Protection
What is the cause of the error in this code?
class DataHolder:
    def __init__(self):
        self.__info = 123

obj = DataHolder()
print(obj.__info)
ATypeError because __info is private
BSyntaxError due to double underscores in attribute name
CAttributeError because __info is name-mangled and not accessible as __info
DNo error; prints 123
Step-by-Step Solution
Solution:
  1. Step 1: Understand name mangling effect

    Attribute __info is internally renamed to _DataHolder__info.
  2. Step 2: Accessing attribute

    Trying to access obj.__info raises AttributeError because __info does not exist as such.
  3. Final Answer:

    AttributeError because __info is name-mangled and not accessible as __info -> Option C
  4. Quick Check:

    Direct access to double underscore attribute fails [OK]
Quick Trick: Double underscore attributes are mangled, direct access fails [OK]
Common Mistakes:
MISTAKES
  • Assuming double underscore causes syntax error
  • Thinking attribute is accessible directly
  • Confusing private with inaccessible

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes