Courseiva

PCAP Object-Oriented Programming Practice Question

When a class defines both __getattr__ and __getattribute__, which one is called when accessing an attribute that exists in the instance?

⚠ Common exam trap

Python Institute often tests the misconception that __getattr__ is the primary hook for attribute access, when in fact __getattribute__ is always called first and __getattr__ is only a fallback for missing attributes.

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

✓

__getattribute__ is called, and __getattr__ is not called.

In Python, when both __getattr__ and __getattribute__ are defined in a class, __getattribute__ is always called first for every attribute access. If the attribute exists in the instance (e.g., in the instance dictionary or via a descriptor), __getattribute__ returns it directly, and __getattr__ is never invoked. __getattr__ is only called as a fallback when __getattribute__ raises an AttributeError. Therefore, for an existing attribute, only __getattribute__ runs, making option D correct.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    __getattr__ always overrides __getattribute__.

    Why it's wrong here

    __getattr__ does not take precedence over __getattribute__. Every attribute access first invokes __getattribute__; only if that method raises AttributeError does Python fall back to __getattr__. Thus __getattr__ is a fallback mechanism, not an override, and it never gets a chance for attributes that __getattribute__ successfully resolves.

  • ✗

    Both __getattr__ and __getattribute__ are called, in that order.

    Why it's wrong here

    This option is misleading because the order is actually reversed: __getattribute__ is always called first, and __getattr__ is called only when __getattribute__ raises AttributeError. For existing attributes, __getattr__ is never called at all, so the blanket statement that both are called is false. The described order would only apply to a missing attribute, and even then it would be '__getattribute__ then __getattr__'.

  • ✗

    Only __getattr__ is called.

    Why it's wrong here

    This option is false because __getattribute__ is the primary hook that intercepts every dot-style attribute access. When __getattribute__ is defined, it runs first, and __getattr__ cannot execute unless __getattribute__ explicitly raises AttributeError. Therefore, __getattr__ is not the only method called; it is at most a secondary handler for failed lookups.

  • ✓

    __getattribute__ is called, and __getattr__ is not called.

    Why this is correct

    For a normal attribute that exists on the instance or class, __getattribute__ is invoked and successfully returns the value, so Python never proceeds to the __getattr__ fallback. This is the correct behavior when a class defines both hooks: the primary lookup method handles the access, and __getattr__ is reserved for cases where the attribute is genuinely missing.

About these practice questions

Courseiva writes every PCAP question from scratch — 421 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

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.