Author Dusty Phillips Reviewers



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

[
 81 
]
Since the 
Wav
class fails to implement the abstract attributes, it is not possible to 
instantiate that class. The class is still a legal abstract class, but you'd have to subclass it 
to actually do anything. The 
Ogg
class supplies both attributes, so it instantiates cleanly.
Going back to the 
MediaLoader
ABC, let's dissect that 
__subclasshook__
method. 
It is basically saying that any class that supplies concrete implementations of all the 
abstract attributes of this ABC should be considered a subclass of 
MediaLoader
,
even if it doesn't actually inherit from the 
MediaLoader
class.
More common object-oriented languages have a clear separation between the 
interface and the implementation of a class. For example, some languages provide
an explicit 
interface
keyword that allows us to define the methods that a class 
must have without any implementation. In such an environment, an abstract class is 
one that provides both an interface and a concrete implementation of some but not 
all methods. Any class can explicitly state that it implements a given interface.
Python's ABCs help to supply the functionality of interfaces without compromising
on the benefits of duck typing.
Demystifying the magic
You can copy and paste the subclass code without understanding it if you want
to make abstract classes that fulfill this particular contract. We'll cover most of 
the unusual syntaxes throughout the book, but let's go over it line by line to get
an overview.
@classmethod
This decorator marks the method as a class method. It essentially says that the 
method can be called on a class instead of an instantiated object:
def __subclasshook__(cls, C):
This defines the 
__subclasshook__
class method. This special method is called by 
the Python interpreter to answer the question, 
Is the class 
C
 a subclass of this class?
if cls is MediaLoader:
We check to see if the method was called specifically on this class, rather than, say a 
subclass of this class. This prevents, for example, the 
Wav
class from being thought of 
as a parent class of the 
Ogg
class:
attrs = set(dir(C))
www.it-ebooks.info


When Objects Are Alike

Download 2,95 Mb.

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