Author Dusty Phillips Reviewers



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

[
 80 
]
pass
@classmethod
def __subclasshook__(cls, C):
if cls is MediaLoader:
attrs = set(dir(C))
if set(cls.__abstractmethods__) <= attrs:
return True
return NotImplemented
This is a complicated example that includes several Python features that won't be 
explained until later in this book. It is included here for completeness, but you don't 
need to understand all of it to get the gist of how to create your own ABC.
The first weird thing is the 
metaclass
keyword argument that is passed into the 
class where you would normally see the list of parent classes. This is a rarely used 
construct from the mystic art of metaclass programming. We won't be covering 
metaclasses in this book, so all you need to know is that by assigning the 
ABCMeta
metaclass, you are giving your class superpower (or at least superclass) abilities.
Next, we see the 
@abc.abstractmethod
and 
@abc.abstractproperty
constructs. 
These are Python decorators. We'll discuss those in 
Chapter 5

When to Use
Object-oriented Programming
. For now, just know that by marking a method or 
property as being abstract, you are stating that any subclass of this class must 
implement that method or supply that property in order to be considered a
proper member of the class.
See what happens if you implement subclasses that do or don't supply
those properties:
>>> class Wav(MediaLoader):
... pass
...
>>> x = Wav()
Traceback (most recent call last):
File "", line 1, in
TypeError: Can't instantiate abstract class Wav with abstract methods 
ext, play
>>> class Ogg(MediaLoader):
... ext = '.ogg'
... def play(self):
... pass
...
>>> o = Ogg()
www.it-ebooks.info


Chapter 3

Download 2,95 Mb.

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