Case study
For this case study, we'll try to delve further into the question, When should I choose an object versus a built-in type? We'll be modeling a Document
class that might be used in a text editor or word processor. What objects, functions, or properties should it have?
We might start with a str
for the Document
contents, but in Python, strings aren't mutable (able to be changed). Once a str
is defined, it is forever. We can't insert a character into it or remove one without creating a brand new string object. That would be leaving a lot of str
objects taking up memory until Python's garbage collector sees fit to clean up behind us.
So, instead of a string, we'll use a list of characters, which we can modify at will. In addition, we'll need to know the current cursor position within the list, and should probably also store a filename for the document.
Note
Real text editors use a binary tree-based data structure called a rope
to model their document contents. This book's title isn't...