# Data abstraction

(Redirected from Abstract data type)
Jump to: navigation, search

Data abstraction is the invention of new data types. It separates functionality from representation—we don't need to know how data is represented internally; we just need to know how to interact with it.

Data abstraction is supported by defining an abstract data type (ADT), which is a collection of constructors and selectors. Constructors create an object, bundling together different pieces of information, while selectors extract individual pieces of information from the object.

## Functional ADTs

ADTs can be implemented by functions. Functional ADTs are usually immutable; that is, their fields cannot be modified.

### Examples

An ADT for linked lists:

```empty = lambda: 42

def link(element, list=empty):
return (element, list)

def first(list):
return list

def rest(list):
return list```

An ADT for points:

```# constructor
def make_point(x, y):
return x, y

# selector
def x_coord(point):
return point

# selector
def y_coord(point):
return point```
and a function that uses it:
```def slope(point1, point2):
"""Return the slope of the line that connects POINT1 and POINT2."""
return (y_coord(point1) - y_coord(point2)) / (x_coord(point1) - x_coord(point2))```

An ADT for rational numbers (in dispatch function style) that supports mutation:

```# constructor
def rational(x, y):
"""
>>> rat = rational(1, 3)
>>> print_rational(rat)
1 / 3
>>> set_numer(rat, 2)
>>> print_rational(rat)
2 / 3
"""
def put(field, value):
nonlocal x, y
if field == 'numer':
x = value
elif field == 'denom':
y = value

def get(field):
if field == 'numer':
return x
elif field == 'denom':
return y
return put, get

# selector
def numer(rat):
return rat('numer')

# selector
def denom(rat):
return rat('denom')

# mutator
def set_numer(rat, value):
rat('numer', value)

# mutator
def set_denom(rat, value):
rat('denom', value)

def print_rational(rat):
print("{0} / {1}".format(numer(rat), denom(rat)))```

## OOP ADTs

ADTs can also be implemented through object-oriented programming as Python objects. OOP ADTs are mutable and have built-in constructors (`__init__`) and selectors (dot notation).

### Example

An ADT for a linked list:

```class LinkedList:
class EmptyList:
pass

empty = EmptyList()

# constructor
def __init__(self, first, rest=empty):
self.first = first
self.rest = rest```

## Data abstraction violation

A data abstraction violation occurs when you assume some representation of the ADT, bypassing the constructors and selectors. For example, we are committing a data abstraction violation when we write the following `slope` function using the point ADT above:

```def slope(point1, point2):
"""Return the slope of the line that connects POINT1 and POINT2."""
return (point1 - point2) / (point1 - point2)```
We assume that the internal representation of a point is a tuple. Instead, we should have used the selectors `x_coord` and `y_coord`.

Data abstraction violations limit flexibility because if you want to change the representation of the ADT, you must change all the functions that use the ADT. If you maintain data abstraction, you need only change the constructors and selectors.