Courseiva

PCAP Object-Oriented Programming Practice Question

A programmer wants to restrict a class to only allow specific attribute names and reduce memory usage. Which feature should they use?

⚠ Common exam trap

Python Institute often tests the misconception that `__slots__` is only about restricting attribute names, but the trap here is that candidates may overlook its primary purpose of memory optimization, leading them to choose options like `@property` or `__init_subclass__` that address access control but not memory reduction.

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

✓

Define `__slots__` as a tuple of allowed attribute names.

Defining `__slots__` as a tuple of allowed attribute names restricts the class to only those attributes, preventing the creation of a per-instance `__dict__` and thereby reducing memory usage. This is a built-in Python mechanism that overrides the default dynamic attribute storage, making it ideal for memory-constrained applications.

Answer analysis

Option-by-option breakdown

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

  • ✓

    Define `__slots__` as a tuple of allowed attribute names.

    Why this is correct

    Defining `__slots__` as a tuple of allowed attribute names is the correct approach because it creates internal descriptors for only those names and suppresses the automatic per-instance `__dict__`. As a result, assigning any attribute not listed in the tuple raises `AttributeError`, so the class is strictly limited to the declared fields. This also reduces memory overhead, which is a useful side effect, but the restriction is enforced at the language level.

  • ✗

    Override `__init_subclass__` to enforce restrictions.

    Why it's wrong here

    Overriding `__init_subclass__` only controls the creation and configuration of subclasses; it is invoked when a subclass is defined, not when attributes are assigned to an instance. Even if it were used to validate class bodies, it would have no influence on `instance.arbitrary = value` assignments, so it cannot restrict instance attribute creation. Therefore, it targets the wrong phase of the object's lifecycle.

  • ✗

    Use @property for every attribute.

    Why it's wrong here

    Using `@property` for every attribute gives you controlled getter/setter logic for those exact names, but it does nothing to stop Python from adding a new, undeclared attribute to an instance. A property is just a managed descriptor on the class; assigning to a name that is not a property still falls through to the instance's `__dict__` and succeeds. To truly block arbitrary attributes, you need something that removes or overrides the default `__dict__` storage, such as `__slots__`.

  • ✗

    Define `__dict__` as a class variable.

    Why it's wrong here

    Defining `__dict__` as a class variable would not restrict attributes; instead, it would attempt to replace the special attribute-storage mapping with a normal class attribute, potentially breaking normal attribute access because instance `__dict__` behavior is implemented by a special descriptor. In practice, assigning `instance.x = 1` would either create or mutate an instance dictionary, but the class-level `__dict__` name does not function as a restriction mechanism and can cause `TypeError` or inconsistent behavior. This option confuses the storage mechanism with a whitelist.

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 →

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.