Handling AttributeError for Missing Attributes
Exhibit
class MyClass:
def __init__(self, x):
self.__x = x
def get_x(self):
return self.__x
obj = MyClass(10)
print(obj.__x)Refer to the exhibit. What will happen when the code is executed?
⚠ Common exam trap
Python Institute often tests the distinction between AttributeError and TypeError, trapping candidates who confuse missing methods with type mismatches.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
It raises an AttributeError
The code attempts to call a method or access an attribute that does not exist on the object. In Python, when you try to access an attribute or method that is not defined on an object, an AttributeError is raised. Option C is correct because the exhibit shows an attempt to call a non-existent method or attribute on an instance, which triggers AttributeError.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
It prints None
Why it's wrong here
The code does not print None because the expression obj.__x never evaluates to None. After the class definition, the attribute __x is internally renamed to _ClassName__x due to name mangling, so obj.__x refers to a nonexistent attribute. Python then raises an AttributeError during attribute lookup rather than returning None, so no value is passed to print().
- ✗
It raises a TypeError
Why it's wrong here
A TypeError is not raised because the failure here is not about applying an incompatible operation or type. Name mangling renames the attribute to _ClassName__x, so obj.__x triggers an attribute lookup that fails for a missing name, which is an AttributeError. No arithmetic, concatenation, or function call is attempted, so the exception type is incorrect.
- ✓
It raises an AttributeError
Why this is correct
The correct outcome is an AttributeError because of Python name mangling. Any identifier of the form __x (two leading underscores, at most one trailing underscore) inside a class is rewritten to _ClassName__x. Since the object has an attribute _ClassName__x and not __x, the direct access obj.__x cannot find the attribute, and Python raises AttributeError with a message like 'Test' object has no attribute '__x'.
- ✗
It prints 10
Why it's wrong here
It does not print 10 because the attribute is not stored under the name __x. Name mangling transforms __x into _ClassName__x, so obj.__x points to a nonexistent attribute and causes an AttributeError before any value could be printed. The value 10 would only be accessible via the mangled name obj._ClassName__x, not through obj.__x.
Go deeper
Related to this question
About these practice questions
One of 421 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.