Author Dusty Phillips Reviewers


super().__init__(name, email)



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

super().__init__(name, email)
self.phone = phone
This example first gets the instance of the parent object using 
super
, and calls 
__init__
on that object, passing in the expected arguments. It then does its own 
initialization, namely, setting the 
phone
attribute.
Note that the 
super()
syntax does not work in older versions of 
Python. Like the [] and {} syntaxes for lists and dictionaries, it is a 
shorthand for a more complicated construct. We'll learn more about 
this shortly when we discuss multiple inheritance, but know for now 
that in Python 2, you would have to call 
super(EmailContact, 
self).__init__()
. Specifically notice that the first argument is the 
name of the child class, not the name as the parent class you want to 
call, as some might expect. Also, remember the class comes before the 
object. I always forget the order, so the new syntax in Python 3 has 
saved me hours of having to look it up.
www.it-ebooks.info


Chapter 3
[
 65 
]

super()
call can be made inside any method, not just 
__init__
. This means all 
methods can be modified via overriding and calls to 
super
. The call to 
super
can 
also be made at 
any point in the method; we don't have to make the call as the first 
line in the method. For example, we may need to manipulate or validate incoming 
parameters before forwarding them to the superclass.
Multiple inheritance
Multiple inheritance is a touchy subject. In principle, it's very simple: a subclass that 
inherits from more than one parent class is able to access functionality from both of 
them. In practice, this is less useful than it sounds and many expert programmers 
recommend against using it.
As a rule of thumb, if you think you need multiple inheritance, you're 
probably wrong, but if you know you need it, you're probably right.
The simplest and most useful form of multiple inheritance is called a 
mixin
. A mixin 
is generally a superclass that is not meant to exist on its own, but is meant to be 
inherited by some other class to provide extra functionality. For example, let's say 
we wanted to add functionality to our 
Contact
class that allows sending an e-mail 
to 
self.email
. Sending e-mail is a common task that we might want to use on many 
other classes. So, we can write a simple mixin class to do the e-mailing for us:
class MailSender:
def send_mail(self, message):
print("Sending mail to " + self.email)
# Add e-mail logic here
For brevity, we won't include the actual e-mail logic here; if you're interested in 
studying how it's done, see the 
smtplib
module in the Python standard library.
This class doesn't do anything special (in fact, it can barely function as a standalone 
class), but it does allow us to define a new class that describes both a 
Contact
and

MailSender
, using multiple inheritance:
class EmailableContact(Contact, MailSender):
pass
The syntax for multiple inheritance looks like a parameter list in the class definition. 
Instead of including one base class inside the parentheses, we include two (or more), 
separated by a comma. We can test this new hybrid to see the mixin at work:

Download 2,95 Mb.

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