Courseiva

PCAP Object-Oriented Programming Practice Question

A class has an attribute that should be computed on access and cached for subsequent accesses. Which pattern is most appropriate?

⚠ Common exam trap

Python Institute often tests the distinction between descriptors that compute on every access versus those that cache, and the trap here is that candidates confuse __slots__ (memory optimization) with caching, or think that class-level methods like @classmethod can somehow provide per-instance caching.

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

✓

Use a custom descriptor that caches the value in the instance's __dict__.

A custom descriptor with a __get__ method can compute the attribute value on first access and store it in the instance's __dict__ for subsequent accesses. This pattern, often called a cached property, ensures the computation happens only once per instance, which is exactly what the question requires.

Answer analysis

Option-by-option breakdown

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

  • ✗

    Use __slots__ to define the attribute.

    Why it's wrong here

    __slots__ only reserves a fixed set of attribute names and strips per-instance __dict__ storage; it provides no mechanism for lazy computation or caching. Even if you list the attribute in __slots__, you still have to compute and assign it eagerly somewhere, and with no __dict__ you lose the natural place to store a cached result. Therefore it solves neither the computed-on-access nor the caching requirement.

  • ✓

    Use a custom descriptor that caches the value in the instance's __dict__.

    Why this is correct

    A custom descriptor that implements __get__ is the right approach because it can compute the value on first access and then store that computed result in the instance's __dict__ under a private key or even the same name if it is a non-data descriptor. On subsequent accesses, the instance dictionary takes precedence over the non-data descriptor, so the cached value is returned directly without recomputation. This exactly matches the desired lazy, one-time computation behavior.

  • ✗

    Use a @classmethod to compute the value.

    Why it's wrong here

    A @classmethod receives the class object as its first argument, not the instance, so it cannot compute a value that depends on instance state, nor can it cache anything in the instance's __dict__. It could only produce a single value shared across all instances, which defeats the purpose of a per-instance computed attribute. This makes it categorically unable to act as an instance-attribute descriptor.

  • ✗

    Use a @staticmethod to compute the value.

    Why it's wrong here

    A @staticmethod receives no implicit first argument at all, so it has no access to either the instance or the class; it is just a plain function attached to the class namespace. It cannot query instance variables to compute a value, nor can it store a result in the instance's __dict__. Because it lacks any descriptor-based __get__ behavior tied to an instance, it can never provide lazy per-instance caching.

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.