MCS 260 Fall 2021
All of this is Python-specific. Some other languages use the term interface for a similar concept, though the details differ quite a bit.
In Python, a sequence is an ordered container supporting access to its items by 0-based index.
Things you can do with a sequence
seq = val(only available if mutable)
for item in seq:
If you create a class with the following methods, it can be used as a sequence:
These methods form the sequence protocol.
To make a sequence mutable, add one more method:
A geometric sequence (or geometric progression) is a sequence of numbers where the ratio between neighboring terms is constant.
Infinite example: $1,2,4,8,16,32,64,\ldots$
Finite example: $5,15,45,135,405$
Let's make a class
FGS to represent a finite geometric sequence.
We'll keep track of
Indexing will be used to request an item from the sequence, which will be computed when needed but not stored.
Let's support item assignment with
Adopt these conventions:
startthe same but adjusts
Still more can be found in the collections.abc module, which contains classes you can subclass when implementing the protocols.