this class allows us to create characters and prefix them with special character when the str(function is applied to them nothing too exciting there we only have to make few minor modifications to the document and cursor classes to work with this class in the document classwe add these two lines at the beginning of the insert methoddef insert(selfcharacter)if not hasattr(character'character')character character(characterthis is rather strange bit of code its basic purpose is to check whether the character being passed in is character or str if it is stringit is wrapped in character class so all objects in the list are character objects howeverit is entirely possible that someone using our code would want to use class that is neither character nor stringusing duck typing if the object has character attributewe assume it is "character-likeobject but if it does notwe assume it is "str-likeobject and wrap it in character this helps the program take advantage of duck typing as well as polymorphismas long as an object has character attributeit can be used in the document class this generic check could be very usefulfor exampleif we wanted to make programmer' editor with syntax highlightingwe' need extra data on the charactersuch as what type of syntax token the character belongs to note that if we are doing lot of this kind of comparisonit' probably better to implement character as an abstract base class with an appropriate __subclasshook__as discussed in when objects are alike in additionwe need to modify the string property on document to accept the new character values all we need to do is call str(on each character before we join it@property def string(self)return "join((str(cfor in self characters)this code uses generator expressionwhich we'll discuss in the iterator pattern it' shortcut to perform specific action on all the objects in sequence finallywe also need to check character characterinstead of just the string character we were storing beforein the home and end functions when we're looking to see whether it matches newline characterdef home(self)while self document charactersself position- character !'\ 'self position - if self position =