Edexcel International Advanced Level Computer Science, point 10.1.1
Abstraction means focusing on what matters and hiding the detail that doesn't. In Unit 2 you must be able to use subprograms to hide implementation details.
The idea in code
A subprogram gives a name to a set of steps. The code that calls it only needs to know:
- What it does (its name and purpose).
- What it needs (its parameters).
- What it gives back (its return value).
It doesn't need to know how it works inside. This is procedural abstraction.
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
# the caller doesn't care HOW is_prime works
if is_prime(29):
print("29 is prime")
If the inside is improved (a faster method), the callers don't change, as long as the interface stays the same.
Built-in abstraction
You use abstraction constantly:
len(items): you don't know how the length is found.sorted(items): you don't write the sorting algorithm.random.randint(1, 6): you don't implement a random number generator.open(),print(): the operating system's details are hidden.
Levels of abstraction
Code can be organised in layers, each using the one below:
def main():
scores = load_scores("scores.txt") # high level: what to do
best = find_best(scores)
show_winner(best)
def find_best(scores):
# lower level: how to do it
best = scores[0]
for s in scores:
if s["points"] > best["points"]:
best = s
return best
main reads like a description of the task. The detail is hidden in the subprograms.
Hiding data structures
Even the data structure can be hidden. Code that uses a stack only needs push and pop, not whether it's a list or a linked list (8.2.5).
class Stack:
def __init__(self):
self._items = [] # hidden detail (a list)
def push(self, item):
self._items.append(item)
def pop(self):
return self._items.pop()
(The underscore signals that _items is internal.)
Benefits
- Simpler to understand: work at one level at a time.
- Easier to change: fix or improve the inside without altering the callers.
- Reuse: the same subprogram is used in many places.
- Teamwork: agree the interface, then build the inside separately.
- Easier to test: each subprogram on its own.
Worked example
Task: a program reports whether a year is a leap year, and prints the days in a month. Use subprograms to hide the detail.
def is_leap_year(year):
return (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
def days_in_month(month, year):
if month == 2:
return 29 if is_leap_year(year) else 28
if month in (4, 6, 9, 11):
return 30
return 31
def main():
year = 2024
for month in range(1, 13):
print(month, days_in_month(month, year))
main()
maindoesn't contain any leap-year rule: it just callsdays_in_month.days_in_monthdoesn't know how a leap year is decided: it callsis_leap_year.- If the leap-year rule needed changing, only
is_leap_yearwould change.
Exam tip: say that abstraction hides implementation details behind a subprogram, so that the caller only needs to know what it does. Give a specific example from the code (such as "main calls is_prime without knowing how it works"). In Unit 2, write small, well-named subprograms.
Check yourself
How does a subprogram provide abstraction?
It hides the steps behind a name, so callers only need to know what it does, what it needs and what it returns, not how it works inside.
Give an example of a built-in function that hides its implementation
For example len(), sorted() or random.randint(): you use them without knowing how they work.
Why can the inside of a subprogram be changed without changing the code that calls it?
The callers only depend on the interface (name, parameters and return value), which stays the same.
Give two benefits of abstraction in code
Any two of: simpler to understand; easier to change; reuse; teamwork; easier testing.