Question 174 of 169
PCAP Object-Oriented Programming Practice Question
A developer wants a class 'LoggedDict' that behaves like a dict but logs all attribute access in the console. Which method override correctly implements this for getting an attribute?
⚠ Common exam trap
Python Institute often tests the distinction between `__getattribute__` (called for every attribute access) and `__getattr__` (called only as a fallback when the attribute is not found), leading candidates to mistakenly choose `__getattr__` because it seems simpler or because they confuse it with the general 'get attribute' concept.
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
✓
def __getattribute__(self, name): print(f'Access {name}'); return super().__getattribute__(name)
`__getattribute__` is the universal method called for every attribute access on an object. By overriding it, the developer can log the attribute name before delegating to the superclass implementation via `super().__getattribute__(name)`, which preserves the normal attribute lookup chain. This ensures that all attribute accesses (including those that exist and those that don't) are logged, which is the requirement for 'LoggedDict'.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
def __get__(self, instance, owner): print(f'Access'); return self
Why it's wrong here
The __get__ method belongs to the descriptor protocol: it is only triggered when the class containing it is used as a class attribute and that attribute is accessed as an instance attribute, not for every attribute lookup. It also has the wrong signature for intercepting general access—it would need to receive the instance and owner class, and simply returning self would return the method object itself, not the attribute's value. Thus it would not log ordinary reads on a loggeddict instance.
- ✓
def __getattribute__(self, name): print(f'Access {name}'); return super().__getattribute__(name)
Why this is correct
This correctly overrides __getattribute__, which Python invokes for every normal attribute access using dot notation on an instance. It prints the attribute name, then delegates to super().__getattribute__(name) to perform the real lookup—this call to the parent implementation is essential both to return the actual attribute value and to avoid infinite recursion. In a loggeddict subclass, this will log access to keys like obj.name, obj.method, and inherited attributes, though implicit special-method calls may bypass it.
- ✗
def __getattr__(self, name): print(f'Access {name}'); return self.__dict__[name]
Why it's wrong here
__getattr__ is a fallback hook, not a general interceptor: it is invoked only after normal attribute lookup has already failed, so existing attributes—exactly the ones that should be logged—never pass through it. Moreover, if a name is truly missing, this implementation attempts self.__dict__[name] and raises KeyError instead of AttributeError, which breaks the standard behavior callers expect when checking for missing attributes. Therefore it cannot serve as a complete access log.
- ✗
def __getitem__(self, key): print(f'Access {key}'); return dict.__getitem__(self, key)
Why it's wrong here
__getitem__ is the protocol for container indexing with square brackets—obj[key]—completely separate from attribute access via the dot operator. In a dict subclass, overriding __getitem__ would log dictionary key lookups such as logged_dict['foo'], but it would never fire for reads like logged_dict.foo. The dictionary key indexing and attribute access use distinct lookup mechanisms, so this method addresses a different operation than the one being requested.
About these practice questions
Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →
Last reviewed: Jun 30, 2026
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.
Question Discussion
Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.
Sign in to join the discussion.