Author Dusty Phillips Reviewers



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

[
 78 
]
Abstract base classes
While duck typing is useful, it is not always easy to tell in advance if a class is going 
to fulfill the protocol you require. Therefore, Python introduced the idea of abstract 
base classes. 
Abstract base classes
, or 
ABCs
, define a set of methods and properties 
that a class must implement in order to be considered a duck-type instance of that 
class. The class can extend the abstract base class itself in order to be used as an 
instance of that class, but it must supply all the appropriate methods.
In practice, it's rarely necessary to create new abstract base classes, but we may find 
occasions to implement instances of existing ABCs. We'll cover implementing ABCs 
first, and then briefly see how to create your own if you should ever need to.
Using an abstract base class
Most of the abstract base classes that exist in the Python Standard Library live in the 
collections
module. One of the simplest ones is the 
Container
class. Let's inspect 
it in the Python interpreter to see what methods this class requires:
>>> from collections import Container
>>> Container.__abstractmethods__
frozenset(['__contains__'])
So, the 
Container
class has exactly one abstract method that needs to be 
implemented, 
__contains__
. You can issue 
help(Container.__contains__)
to see what the function signature should look like:
Help on method __contains__ in module _abcoll: 
__contains__(self, x) unbound _abcoll.Container method
So, we see that 
__contains__
needs to take a single argument. Unfortunately, the 
help file doesn't tell us much about what that argument should be, but it's pretty 
obvious from the name of the ABC and the single method it implements that this 
argument is the value the user is checking to see if the container holds.
This method is implemented by 
list

str
, and 
dict
to indicate whether or not a 
given value is in that data structure. However, we can also define a silly container 
that tells us whether a given value is in the set of odd integers:
class OddContainer:
def __contains__(self, x):
if not isinstance(x, int) or not x % 2:
return False
return True
www.it-ebooks.info


Chapter 3

Download 2,95 Mb.

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