Chapter 1: Software refactoring exercise

Warming up

face Josiah Wang Rob Chatley

Download sequence.py. Examine the code.

  • There are two classes: FibonacciSequence and TriangleNumberSequence. Try to understand what each class does.
  • What does the term() method of each class return?
  • How is the length attribute used in each class?
  • The __iter__() and __next__() methods allow Python to treat these classes as iterable sequences.
  • Look at the test code at the end of the script to get a feel of how both classes are used.

Run the code (python3 sequence.py). It should work and generate the correct sequences.