Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet173/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   169   170   171   172   173   174   175   176   ...   183
Bog'liq
python3-oop

c = a.add(b)
l[0] = 5
l.setitem(0, 5)
d[key] = value
d.setitem(key, value)
for x in alist:
#do something with x
it = alist.iterator()
while it.has_next():
x = it.next()
#do something with x
The highlighted sections show what object-oriented code might look like (in practice, 
these methods actually exist as special double-underscore methods on associated 
objects). Python programmers agree that the non-object-oriented syntax is easier 
both to read and to write. Yet all of the preceding Python syntaxes map to object-
oriented methods underneath the hood. These methods have special names (with 
double-underscores before and after) to remind us that there is a better syntax out 
there. However, it gives us the means to override these behaviors. For example, we 
can make a special integer that always returns 
0
when we add two of them together:
class SillyInt(int):
def __add__(self, num):
return 0
This is an extremely bizarre thing to do, granted, but it perfectly illustrates these 
object-oriented principles in action:
>>> a = SillyInt(1)
>>> b = SillyInt(2)
>>> a + b
0
www.it-ebooks.info


Chapter 6
[
 179 
]
The awesome thing about the 
__add__
method is that we can add it to any class we 
write, and if we use the 
+
operator on instances of that class, it will be called. This is 
how string, tuple, and list concatenation works, for example.
This is true of all the special methods. If we want to use 
x
in
myobj
syntax for 
a custom-defined object, we can implement 
__contains__
. If we want to use 
myobj[i]
=
value
syntax, we supply a 
__setitem__
method and if we want to
use 
something
=
myobj[i]
, we implement 
__getitem__
.
There are 33 of these special methods on the 
list
class. We can use the 
dir
function to see all of them:

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   169   170   171   172   173   174   175   176   ...   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