Courseiva
Object-Oriented ProgramminghardMultiple ChoiceObjective-mapped

Implementing Sequence Protocol for Slicing

You are designing a class that should behave like a sequence and support slicing. Which special methods must be implemented?

Quick Answer

The answer is `__getitem__` and `__len__`. These two special methods form the core of the sequence protocol in Python, which is what enables slicing behavior like `obj[start:stop:step]`. The `__getitem__` method must handle both integer indices and slice objects, while `__len__` provides the sequence’s length, which Python uses internally to compute proper slice boundaries and defaults. On the Certified Associate Python Programmer PCAP exam, this topic tests your understanding of how Python’s data model supports custom sequences—a common trap is thinking that only `__getitem__` is needed, but without `__len__`, slicing can produce incorrect or unexpected results, especially with negative indices or omitted bounds. A reliable memory tip is to remember that “slicing needs both a map and a measure”: `__getitem__` maps indices to values, and `__len__` measures the total size, so Python knows where to start and stop.

⚠ Common exam trap

Python Institute often tests the misconception that `__getitem__` alone is enough for slicing, but the trap is that `__len__` is also required for the interpreter to handle slice defaults and negative indices correctly.

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

__getitem__ and __len__

For a class to support slicing in Python, it must implement both `__getitem__` (to handle indexing and slice objects) and `__len__` (to define the sequence length, which is required for proper slice boundary handling). Together, these satisfy the sequence protocol, enabling Python's slicing syntax like `obj[start:stop:step]`.

Answer analysis

Option-by-option breakdown

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

  • __len__ and __contains__

    Why it's wrong here

    __contains__ is for membership, not slicing.

  • __iter__ and __next__

    Why it's wrong here

    These support iteration, not slicing directly.

  • __getitem__ alone

    Why it's wrong here

    While __getitem__ is essential, sequences are expected to have __len__ as well.

  • __getitem__ and __len__

    Why this is correct

    These two methods are the minimum for a sequence that supports slicing.

  • __getitem__ and __setitem__

    Why it's wrong here

    __setitem__ is for item assignment, not necessary for read-only sequences.

About these practice questions

Courseiva writes every PCAP question from scratch — 169 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

Same concept, more angles

1 more way this is tested on PCAP

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. A Python developer is implementing a class that should behave like a sequence and support indexing. Which pair of special methods must be defined to achieve this?

hard
  • A.__getitem__ and __contains__
  • B.__setitem__ and __delitem__
  • C.__iter__ and __next__
  • D.__getitem__ and __len__

Why D: To make a class behave like a sequence and support indexing (e.g., obj[0]), Python requires the __getitem__ method to retrieve items by key. Additionally, the __len__ method is needed to define the length of the sequence, which is used by built-in functions like len() and is part of the sequence protocol. Together, these two methods satisfy the minimal requirements for a sequence-like object that supports indexing.

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.