Author Dusty Phillips Reviewers


>>> longkeys = LongNameDict()



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

>>> longkeys = LongNameDict()
>>> longkeys['hello'] = 1
>>> longkeys['longest yet'] = 5
>>> longkeys['hello2'] = 'world'
>>> longkeys.longest_key()
'longest yet'
Most built-in types can be similarly extended. Commonly extended built-ins are 
object

list

set

dict

file
, and 
str
. Numerical types such as 
int
and 
float
are also occasionally inherited from.
Overriding and super
So, inheritance is great for 
adding
new behavior to existing classes, but what about 
changing
behavior? Our 
contact
class allows only a name and an e-mail address. 
This may be sufficient for most contacts, but what if we want to add a phone 
number for our close friends?
www.it-ebooks.info


When Objects Are Alike
[
 64 
]
As we saw in 
Chapter 2

Objects in Python
, we can do this easily by just setting a 
phone
attribute on the contact after it is constructed. But if we want to make this 
third variable available on initialization, we have to override 
__init__
. Overriding 
means altering or replacing a method of the superclass with a new method (with 
the same name) in the subclass. No special syntax is needed to do this; the subclass's 
newly created method is automatically called instead of the superclass's method. For 
example:
class Friend(Contact):
def __init__(self, name, email, phone):
self.name = name
self.email = email
self.phone = phone
Any method can be overridden, not just 
__init__
. Before we go on, however, we 
need to address some problems in this example. Our 
Contact
and 
Friend
classes 
have duplicate code to set up the 
name
and 
email
properties; this can make code 
maintenance complicated as we have to update the code in two or more places.
More alarmingly, our 
Friend
class is neglecting to add itself to the 
all_contacts
list we have created on the 
Contact
class.
What we really need is a way to execute the original 
__init__
method on the 
Contact
class. This is what the 
super
function does; it returns the object as an 
instance of the parent class, allowing us to call the parent method directly:
class Friend(Contact):
def __init__(self, name, email, phone):

Download 2,95 Mb.

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