Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet77/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   73   74   75   76   77   78   79   80   ...   183
Bog'liq
python3-oop

[
 71 
]
print("Calling method on Base Class")
self.num_base_calls += 1
class LeftSubclass(BaseClass):
num_left_calls = 0
def call_me(self):
super().call_me()
print("Calling method on Left Subclass")
self.num_left_calls += 1
class RightSubclass(BaseClass):
num_right_calls = 0
def call_me(self):
super().call_me()
print("Calling method on Right Subclass")
self.num_right_calls += 1
class Subclass(LeftSubclass, RightSubclass):
num_sub_calls = 0
def call_me(self):
super().call_me()
print("Calling method on Subclass")
self.num_sub_calls += 1
The change is pretty minor; we simply replaced the naive direct calls with calls to 
super()
, although the bottom subclass only calls 
super
once rather than having to 
make the calls for both the left and right. The change is simple enough, but look at 
the difference when we execute it:
>>> s = Subclass()
>>> s.call_me()
Calling method on Base Class
Calling method on Right Subclass
Calling method on Left Subclass
Calling method on Subclass
>>> print(s.num_sub_calls, s.num_left_calls, s.num_right_calls,
s.num_base_calls)
1 1 1 1
Looks good, our base method is only being called once. But what is 
super()
actually 
doing here? Since the 
print
statements are executed after the 
super
calls, the printed 
output is in the order each method is actually executed. Let's look at the output from 
back to front to see who is calling what.
www.it-ebooks.info


When Objects Are Alike
[
 72 
]
First, 
call_me
of 
Subclass
calls 
super().call_me()
, which happens to refer
to 
LeftSubclass.call_me()
. The 
LeftSubclass.call_me()
method then calls 
super().call_me()
, but in this case, 
super()
is referring to 
RightSubclass.call_
me()
.
Pay particular attention to this: the 
super
call is 
not
calling the method on the 
superclass of 
LeftSubclass
(which is 
BaseClass
). Rather, it is calling 
RightSubclass

even though it is not a direct parent of 
LeftSubclass
! This is the 
next
method, not 
the parent method. 
RightSubclass
then calls 
BaseClass
and the 
super
calls have 
ensured each method in the class hierarchy is executed once.

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   73   74   75   76   77   78   79   80   ...   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