Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet74/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   70   71   72   73   74   75   76   77   ...   183
Bog'liq
python3-oop

[
 68 
]
In this example, we directly call the 
__init__
function on each of the superclasses 
and explicitly pass the 
self
argument. This example technically works; we can 
access the different variables directly on the class. But there are a few problems.
First, it is possible for a superclass to go uninitialized if we neglect to explicitly call 
the initializer. That wouldn't break this example, but it could cause hard-to-debug 
program crashes in common scenarios. Imagine trying to insert data into a database 
that has not been connected to, for example.
Second, and more sinister, is the possibility of a superclass being called multiple times 
because of the organization of the class hierarchy. Look at this inheritance diagram:
Friend
+_init_()
Object
+_init_()
Contact
+_init_()
AddressHolder
+_init_ )
The 
__init__
method from the 
Friend
class first calls 
__init__
on 
Contact

which implicitly initializes the 
object
superclass (remember, all classes derive from 
object
). 
Friend
then calls 
__init__
on 
AddressHolder
, which implicitly initializes 
the 
object
superclass 
again
. This means the parent class has been set up twice. With 
the 
object
class, that's relatively harmless, but in some situations, it could spell 
disaster. Imagine trying to connect to a database twice for every request!
The base class should only be called once. Once, yes, but when? Do we call 
Friend

then 
Contact
, then 
Object
, then 
AddressHolder
? Or 
Friend
, then 
Contact
, then 
AddressHolder
, then 
Object
?
The order in which methods can be called can be adapted on the fly 
by modifying the 
__mro__
(

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   70   71   72   73   74   75   76   77   ...   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