Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet172/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   168   169   170   171   172   173   174   175   ...   183
Bog'liq
python3-oop

Extending built-ins
We 
discussed briefly in 
Chapter 3

When Objects Are Alike
, how built-in data types can 
be extended using inheritance. Now, we'll go into more detail as to when we would 
want to do that.
When we have a built-in container object that we want to add functionality to, we 
have two options. We can either create a new object, which holds that container as 
an attribute (composition), or we can subclass the built-in object and add or adapt 
methods on it to do what we want (inheritance).
Composition is usually the best alternative if all we want to do is use the container 
to store some objects using that container's features. That way, it's easy to pass that 
data structure into other methods and they will know how to interact with it. But we 
need to use inheritance if we want to change the way the container actually works. 
For example, if we want to ensure every item in a 
list
is a string with exactly five 
characters, we need to extend 
list
and override the 
append()
method to raise an 
exception for invalid input. We'd also minimally have to override 
__setitem__
(self,
index,
value)
, a special method on lists that is called whenever we use the 
x[index]
=
"value"
syntax, and the 
extend()
method.
www.it-ebooks.info


Python Data Structures
[
 178 
]
Yes, lists are objects. All that special non-object-oriented looking syntax we've
been looking at for accessing lists or dictionary keys, looping over containers,
and similar tasks is actually "syntactic sugar" that maps to an object-oriented 
paradigm underneath. We might ask the Python designers why they did this.
Isn't object-oriented programming 
always
better? That question is easy to answer. 
In the following hypothetical examples, which is easier to read, as a programmer? 
Which requires less typing?
c = a + b

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   168   169   170   171   172   173   174   175   ...   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