Author Dusty Phillips Reviewers


>>> e = EmailableContact("John Smith", "jsmith@example.net")



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

>>> e = EmailableContact("John Smith", "jsmith@example.net")
www.it-ebooks.info


When Objects Are Alike
[
 66 
]
>>> Contact.all_contacts
[<__main__.EmailableContact object at 0xb7205fac>]
>>> e.send_mail("Hello, test e-mail here")
Sending mail to jsmith@example.net
The 
Contact
initializer is still adding the new contact to the 
all_contacts
list, and 
the mixin is able to send mail to 
self.email
so we know everything is working.
This wasn't so hard, and you're probably wondering what the dire warnings
about multiple inheritance are. We'll get into the complexities in a minute, but
let's consider some other options we had, rather than using a mixin here:
• 
We could have used single inheritance and added the 
send_mail
function
to the subclass. The disadvantage here is that the e-mail functionality then 
has to be duplicated for any other classes that need e-mail.
• 
We can create a standalone Python function for sending an e-mail, and just 
call that function with the correct e-mail address supplied as a parameter 
when the e-mail needs to be sent.
• 
We could have explored a few ways of using composition instead of 
inheritance. For example, 
EmailableContact
could have a 
MailSender
object instead of inheriting from it.
• 
We could monkey-patch (we'll briefly cover monkey-patching in 
Chapter 7

Python Object-oriented Shortcuts
) the 
Contact
class to have a 
send_mail
method 
after the class has been created. This is done by defining a function that accepts 
the 
self
argument, and setting it as an attribute on an existing class.
Multiple inheritance works all right when mixing methods from different classes, 
but it gets very messy when we have to call methods on the superclass. There are 
multiple superclasses. How do we know which one to call? How do we know 
what order to call them in?
Let's explore these questions by adding a home address to our 
Friend
class. 
There are a few approaches we might take. An address is a collection of strings 
representing the street, city, country, and other related details of the contact. We 
could pass each of these strings as a parameter into the 
Friend
class's 
__init__
method. We could also store these strings in a tuple or dictionary and pass them
into 
__init__
as a single argument. This is probably the best course of action if
there are no methods that need to be added to the address.
www.it-ebooks.info


Chapter 3

Download 2,95 Mb.

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