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 strings aren't mutable. A mutable object is one that can be changed; but a str
is immutable, we can't insert a character into it or remove one without creating a brand new string object. That's leaving a lot of str
objects for Python's garbage collector 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, a Document
would need to know the current cursor position within the list, and should also store a filename for the document.
Now, what methods should it have? There are a lot of things we might want to do to a text document, including inserting and deleting characters, cut, copy, paste, and...