Author Dusty Phillips Reviewers



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

[
 73 
]
Python's function parameter syntax provides all the tools we need to do this, but it 
makes the overall code look cumbersome. Have a look at the proper version of the 
Friend
multiple inheritance code:
class Contact:
all_contacts = []
def __init__(self, name='', email='', **kwargs):
super().__init__(**kwargs)
self.name = name
self.email = email
self.all_contacts.append(self)
class AddressHolder:
def __init__(self, street='', city='', state='', code='',
**kwargs):
super().__init__(**kwargs)
self.street = street
self.city = city
self.state = state
self.code = code
class Friend(Contact, AddressHolder):
def __init__(self, phone='', **kwargs):
super().__init__(**kwargs)
self.phone = phone
We've changed all arguments to keyword arguments by giving them an empty
string as a default value. We've also ensured that a 
**kwargs
parameter is included 
to capture any additional parameters that our particular method doesn't know what 
to do with. It passes these parameters up to the next class with the 
super
call.
If you aren't familiar with the 
**kwargs
syntax, it basically collects 
any keyword arguments passed into the method that were not 
explicitly listed in the parameter list. These arguments are stored in a 
dictionary named 
kwargs
(we can call the variable whatever we like, 
but convention suggests 
kw
, or 
kwargs
). When we call a different 
method (for example, 
super().__init__
) with a 
**kwargs
syntax, it unpacks the dictionary and passes the results to the method 
as normal keyword arguments. We'll cover this in detail in 
Chapter 7

Python Object-oriented Shortcuts
.
www.it-ebooks.info


When Objects Are Alike

Download 2,95 Mb.

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