Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet181/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   175   176   177   178   179   180   181   182   183
Bog'liq
python3-oop

'one'
>>> stack.empty()
True
>>> stack.get(timeout=1)
Traceback (most recent call last):
File "", line 1, in
stack.get(timeout=1)
File "/usr/lib64/python3.3/queue.py", line 175, in get
raise Empty
queue.Empty
You might wonder why you couldn't just use the 
append()
and 
pop()
methods on a 
standard list. Quite frankly, that's probably what I would do. I rarely have occasion 
to use the 
LifoQueue
class in production code. Working with the end of a list is an 
efficient operation; so efficient, in fact, that the 
LifoQueue
uses a standard list under 
the hood!
There are a couple of reasons that you might want to use 
LifoQueue
instead of a 
list. The most important one is that 
LifoQueue
supports clean concurrent access 
from multiple threads. If you need stack-like behavior in a concurrent setting, you 
should leave the list at home. Second, 
LifoQueue
enforces the stack interface. You 
can't unwittingly insert a value to the wrong position in a 
LifoQueue
, for example 
(although, as an exercise, you can work out how to do this completely wittingly).
Priority queues
The priority queue enforces a very different style of ordering from the previous queue 
implementations. Once again, they follow the exact same 
get()
and 
put()
API, but 
instead of relying on the order that items arrive to determine when they should be 
returned, the most "important" item is returned. By convention, the most important,
or highest priority item is the one that sorts lowest using the less than operator.
A common convention is to store tuples in the priority queue, where the first element 
in the tuple is the priority for that element, and the second element is the data. Another 
common paradigm is to implement the 
__lt__
method, as we discussed earlier in this 
chapter. It is perfectly acceptable to have multiple elements with the same priority in 
the queue, although there are no guarantees on which one will be returned first.
www.it-ebooks.info


Chapter 6

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   175   176   177   178   179   180   181   182   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