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 ...
Get Python 3 Object Oriented Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.