PCAP __slots__ Practice Question
Which of the following correctly uses `__slots__` to restrict attribute creation to only `x` and `y`?
⚠ Common exam trap
Python Institute often tests the misconception that a single string or a parenthesized string without a trailing comma is a valid iterable for `__slots__`, leading candidates to pick options that inadvertently restrict attributes to individual characters rather than the intended attribute names. Additionally, candidates may overlook that a list is also a valid iterable.
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
✓
`class Foo: __slots__ = ['x', 'y']`
Options C and D are both correct because `__slots__` must be assigned an iterable of strings. A list `['x', 'y']` and a tuple `('x', 'y')` are both valid iterables that restrict attribute creation to exactly `x` and `y`. Option A uses a single string, which would restrict to individual characters `'x'`; Option B uses a string in parentheses without a trailing comma, which is also just a string. Both A and B would produce unexpected behavior.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
`class Foo: __slots__ = 'x'`
Why it's wrong here
Incorrect. Assigning a plain string 'x' treats it as an iterable of characters, so only 'x' is allowed as an attribute name? Actually, it would iterate over the string, creating slots for each character: 'x'. This effectively allows only attribute 'x', but the intended behavior is to allow exactly 'x' and 'y'. More importants, a single string does not allow two attributes.
- ✗
`class Foo: __slots__ = ('x')`
Why it's wrong here
Writing ('x') does not create a tuple; parentheses without a comma are just grouping, so the value is the string 'x'. That string is itself an iterable whose elements are characters, and Python will iterate over it to generate slot names. Because the only character is 'x', the class ends up allowing exactly one attribute, not the intended pair, and 'y' is rejected by the descriptor. To define a single-element tuple you must write ('x',).
- ✓
`class Foo: __slots__ = ['x', 'y']`
Why this is correct
A list such as ['x', 'y'] is also a valid iterable, so Python will happily consume it and create exactly those two slots. The interpreter does not require an immutable type for __slots__; it simply iterates over the object to collect the attribute names. However, because the list remains mutable and is stored as a class attribute, a later append or rebind could change the set of allowed attributes, which is why tuples are the conventional, safer choice.
- ✓
`class Foo: __slots__ = ('x', 'y')`
Why this is correct
The tuple ('x', 'y') is a proper iterable sequence of two distinct string field names. When a class declares __slots__, Python reads it as an iterable and allocates each name as a descriptor-based slot, so instances store only 'x' and 'y' and gain no __dict__. The trailing comma after 'y' is what makes this a real tuple; without it the expression would degrade to a plain string.
Go deeper
Related to this question
About these practice questions
This PCAP question is part of Courseiva's 169-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. 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.