>>> print(d.string)
hello
world
This framework is simple (though it might be a bit time consuming!) to extend to
create and edit a complete plaintext document. Now, let's extend it to work for rich
text; text that can have
bold
, underlined, or
italic
characters.
There are two ways we could process this; the first is to insert "fake" characters into
our character list that act like instructions, such as "bold characters until you find a
stop bold character". The second is to add information to each character indicating
what formatting it should have. While the former method is probably more common,
we'll implement the latter solution. To do that, we're obviously going to need a class
for characters. This class will have an attribute representing the character, as well as
three Boolean attributes representing whether it is bold, italic, or underlined.
Hmm, wait! Is this
Character
class going to have any methods? If not, maybe we
should use one of the many Python data structures instead; a tuple or named tuple
would probably be sufficient. Are there any actions that we would want to do to,
or invoke on a character?
www.it-ebooks.info
When to Use Object-oriented Programming
[
150
]
Well, clearly, we might want to do things with characters, such as delete or copy
them, but those are things that need to be handled at the
Document
level, since they
are really modifying the list of characters. Are there things that need to be done to
individual characters?
Actually, now that we're thinking about what a
Character
class actually is... what
is it? Would it be safe to say that a
Character
class is a string? Maybe we should
use an inheritance relationship here? Then we can take advantage of the numerous
methods that
str
instances come with.
What sorts of methods are we talking about? There's
startswith
,
strip
,
find
,
lower
, and many more. Most of these methods expect to be working on strings
that contain more than one character. In contrast, if
Character
were to subclass
str
,
we'd probably be wise to override
__init__
to raise an exception if a multi-character
string were supplied. Since all those methods we'd get for free wouldn't really apply
to our
Character
class, it seems we needn't use inheritance, after all.
This brings us back to our original question; should
Character
even be a class?
There is a very important special method on the
object
class that we can take
advantage of to represent our characters. This method, called
__str__
(two
underscores, like
__init_
_), is used in string manipulation functions like
print
and the
str
constructor to convert any class to a string. The default implementation
does some boring stuff like printing the name of the module and class and its
address in memory. But if we override it, we can make it print whatever we like.
For our implementation, we could make it prefix characters with special characters
to represent whether they are bold, italic, or underlined. So, we will create a class
to represent a character, and here it is:
class Character:
def __init__(self, character,
bold=False, italic=False, underline=False):
assert len(character) == 1
self.character = character
self.bold = bold
self.italic = italic
self.underline = underline
def __str__(self):
bold = "*" if self.bold else ''
italic = "/" if self.italic else ''
underline = "_" if self.underline else ''
return bold + italic + underline + self.character
www.it-ebooks.info
Chapter 5
Do'stlaringiz bilan baham: |