Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet85/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   81   82   83   84   85   86   87   88   ...   183
Bog'liq
python3-oop

[
 79 
]
Now, we can instantiate an 
OddContainer
object and determine that, even though
we did not extend 
Container
, the class 
is a
Container
object:
>>> from collections import Container
>>> odd_container = OddContainer()
>>> isinstance(odd_container, Container)
True
>>> issubclass(OddContainer, Container)
True
And that is why duck typing is way more awesome than classical polymorphism. 
We can create 
is a
relationships without the overhead of using inheritance (or
worse, multiple inheritance).
The interesting thing about the 
Container
ABC is that any class that implements 
it gets to use the 
in
keyword for free. In fact, 
in
is just syntax sugar that delegates 
to the 
__contains__
method. Any class that has a 
__contains__
method is a 
Container
and can therefore be queried by the 
in
keyword, for example:
>>> 1 in odd_container
True
>>> 2 in odd_container
False
>>> 3 in odd_container
True
>>> "a string" in odd_container
False
Creating an abstract base class
As we saw earlier, it's not necessary to have an abstract base class to enable duck 
typing. However, imagine we were creating a media player with third-party plugins. It 
is advisable to create an abstract base class in this case to document what API the third-
party plugins should provide. The 
abc
module provides the tools you need to do this, 
but I'll warn you in advance, this requires some of Python's most arcane concepts:
import abc
class MediaLoader(metaclass=abc.ABCMeta):
@abc.abstractmethod
def play(self):
pass
@abc.abstractproperty
def ext(self):
www.it-ebooks.info


When Objects Are Alike

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   81   82   83   84   85   86   87   88   ...   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