Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet145/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   141   142   143   144   145   146   147   148   ...   183
Bog'liq
python3-oop

>>> 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

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   141   142   143   144   145   146   147   148   ...   183




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish