Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet166/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   162   163   164   165   166   167   168   169   ...   183
Bog'liq
python3-oop

[
 171 
]
>>> for i in l:
... i.sort_num = False
...
>>> l.sort()
>>> l
[a:4, b:3, c:2, d:1]
The first time 
we call 
sort
, it sorts by numbers because 
sort_num
is 
True
on all the 
objects being compared. The second time, it sorts by letters. The 
__lt__
method 
is the only one we need to implement to enable sorting. Technically, however, if it 
is implemented, the class should normally also implement the similar 
__gt__

__
eq__

__ne__

__ge__
, and 
__le__
methods so that all of the 
<

>

==

!=

>=
, and 
<=
operators also work properly. You can get this for free by implementing 
__lt__
and 
__eq__
, and then applying the 
@total_ordering
class decorator to supply the rest:
from functools import total_ordering
@total_ordering
class WeirdSortee:
def __init__(self, string, number, sort_num):
self.string = string
self.number = number
self.sort_num = sort_num
def __lt__(self, object):
if self.sort_num:
return self.number < object.number
return self.string < object.string
def __repr__(self):
return"{}:{}".format(self.string, self.number)
def __eq__(self, object):
return all((
self.string == object.string,
self.number == object.number,
self.sort_num == object.number
))
www.it-ebooks.info


Python Data Structures
[
 172 
]
This is useful if we want to be able to use operators on our objects. However, if all we 
want to do is customize our sort orders, even this is overkill. For such a use case, the 
sort
method can take an optional 
key
argument. This argument is a function that 
can translate each object in a list into an object that can somehow be compared. For 
example, we can use 
str.lower
as the key argument to perform a case-insensitive 
sort on a list of strings:

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   162   163   164   165   166   167   168   169   ...   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