PCAP Object-Oriented Programming Practice Question
A developer defines a class with a private attribute `_value` and wants to provide controlled access. Which approach is the most Pythonic?
⚠ Common exam trap
Python Institute often tests the distinction between Pythonic idioms and patterns borrowed from other languages (like Java), so the trap here is that candidates familiar with Java or C++ may choose Option D (explicit getter/setter methods) because it looks familiar, missing that Python's `@property` is the preferred, more concise approach.
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 @property to define getter and setter methods.
Using the `@property` decorator is the most Pythonic way to implement controlled access to a private attribute. It allows you to define getter and setter methods that can be called like regular attribute access (e.g., `obj.value`), preserving encapsulation while maintaining a clean, non-method-call interface. This approach aligns with Python's philosophy of 'we are all consenting adults' and avoids the verbosity of explicit getter/setter methods.
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 restrict attribute creation.
Why it's wrong here
Using `__slots__` only prevents dynamic creation of new attributes by removing the per-instance `__dict__`; it does not intercept reads or writes to existing attributes. It merely restricts the set of allowed attribute names, so it cannot enforce validation or block direct assignment to `_value`. Thus it fails as an access-control mechanism and would not provide controlled access to a private attribute.
- ✗
Make `_value` public and rely on documentation.
Why it's wrong here
Making `_value` public abandons encapsulation by exposing the attribute to unconditional direct reads and writes. Relying on documentation is a convention, not an enforcement mechanism; any external code can bypass it without error and set `_value` to an invalid state. In Python, which has no access modifiers, the underscore is a deliberate signal that must be backed by actual control, not just documentation.
- ✓
Use @property to define getter and setter methods.
Why this is correct
Using the `@property` decorator is the canonical Pythonic way to encapsulate private attributes because it allows you to define getter and setter methods while preserving plain attribute syntax for callers. The getter can hide internal representation or perform lazy computation, and the setter can validate input before assigning to the underlying `_value`, ensuring invariants hold. This provides controlled access without forcing explicit method calls, striking the right balance between encapsulation and usability.
- ✗
Define `get_value()` and `set_value()` methods.
Why it's wrong here
Defining `get_value()` and `set_value()` methods does provide encapsulation and validation, but it forces all clients to use explicit method-call syntax, which is considered non-Pythonic. The same control can be achieved more cleanly with `@property`, letting code read and write `obj.value` while still invoking the underlying methods. Java-style accessors are therefore viewed as overly verbose and less aligned with Python's descriptor protocol.
Go deeper
Related to this question
About these practice questions
This PCAP question is part of Courseiva's 421-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.