Author Dusty Phillips Reviewers



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

[
 151 
]
This class allows us to create characters and prefix them with a special character 
when the 
str()
function is applied to them. Nothing too exciting there. We only 
have to make a few minor modifications to the 
Document
and 
Cursor
classes to
work with this class. In the 
Document
class, we add these two lines at the
beginning of the 
insert
method:
def insert(self, character):
if not hasattr(character, 'character'):
character = Character(character)
This is a rather strange bit of code. Its basic purpose is to check whether the character 
being passed in is a 
Character
or a 
str
. If it is a string, it is wrapped in a 
Character
class so all objects in the list are 
Character
objects. However, it is entirely possible 
that someone using our code would want to use a class that is neither 
Character
nor string, using duck typing. If the object has a character attribute, we assume it is 
a "
Character
-like" object. But if it does not, we assume it is a "
str
-like" object and 
wrap it in 
Character
. This helps the program take advantage of duck typing as well 
as polymorphism; as long as an object has a character attribute, it can be used in the 
Document
class.
This generic check could be very useful, for example, if we wanted to make a 
programmer's editor with syntax highlighting: we'd need extra data on the character, 
such as what type of syntax token the character belongs to. Note that if we are doing 
a lot of this kind of comparison, it's probably better to implement 
Character
as an 
abstract base class with an appropriate 
__subclasshook__
, as discussed in 
Chapter 3

When Objects Are Alike
.
In addition, we 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):

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   142   143   144   145   146   147   148   149   ...   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