Sub topic 2
What an abstract data type is
This chapter assumes you can read Python classes, know what a list is, and are comfortable with function return values. You don't need more than that.
The definition
An abstract data type (ADT) is a kind of value described only by the operations you can perform on it and the behavior those operations promise. The description says what the operations do. It says nothing about how the data is stored.
An ADT has two parts:
- The operations: their names, the arguments they take, and the results they return.
- The behavior: what each operation guarantees, including what happens in error cases.
Anything beyond that is an implementation detail. The ADT does not say whether the data lives in a list, a chain of nodes, or a dictionary.
An implementation is one way to keep the promise
An implementation is concrete code that stores the data and carries out the operations. One ADT can have many implementations. Each is correct if it keeps every promise in the specification.
The client only touches the middle box. It never reaches into either implementation.
A small example
Take a tally, an ADT with two operations. add() records one event. total() returns how many events were recorded. The promise is that total() equals the number of add() calls so far.
Here are two implementations.
class IntTally:
def __init__(self):
self._count = 0
def add(self):
self._count += 1
def total(self):
return self._count
class MarkTally:
def __init__(self):
self._marks = []
def add(self):
self._marks.append("|")
def total(self):
return len(self._marks)
def count_words(words, tally):
for _ in words:
tally.add()
return tally.total()
for make_tally in (IntTally, MarkTally):
print(count_words(["a", "b", "c"], make_tally()))
# 3
# 3IntTally stores one integer. MarkTally stores a growing list of marks. Their internals differ, but count_words gives the same answer with both. It uses only add and total, so it works with any correct tally.
ADT versus data structure
These two terms are easy to mix up.
| Term | What it describes | Example from above |
|---|---|---|
| ADT | What you can do and what is promised | `add` and `total` with their behavior |
| Data structure | How the data is arranged in memory | An integer, or a list of marks |
The ADT is the contract. The data structure is part of one way to fulfill it.
Why the separation pays off
- You can change the representation, for example to make an operation faster, without editing client code.
- You can reason about client code by reading the specification alone.
- You can test every implementation against the same expectations.
Common mistakes
- Describing an ADT by its storage ("a stack is a list"). A list is one possible implementation, not the definition.
- Leaving behavior unstated. Names alone do not form a contract. If
total()could mean "events since the last reset", the clients cannot rely on it. - Writing client code that checks the type of the implementation, such as
isinstance(tally, IntTally). That ties the client to one representation.
Exercise
A colleague says, "Our ADT is a dictionary, because we store the data in one." What is wrong with this statement, and what would you say instead?
Show answer
A dictionary is a data structure, which is a way to store data. An ADT is defined by its operations and their promised behavior. You could say, "Our ADT offers these operations with this behavior, and one implementation stores the data in a dictionary." That leaves room to swap the dictionary for another representation without changing clients.