Collection which raises exception when exceeding its max size

474 views Asked by At

Is there a collection which raises exception when exceeding its max size property?

I know it happens in the Queue.Queue object but it did not raise exception when I used collections.deque, but instead the last element got overridden.

Thanks in advance!

1

There are 1 answers

1
Daniel On

You can use a Queue which raises queue.Full if full:

>>> import queue
>>> q = queue.Queue(3)
>>> q.put_nowait(1)
>>> q.put_nowait(2)
>>> q.put_nowait(3)
>>> q.put_nowait(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/ruediger/data/p3/Python-3.4.2/Lib/queue.py", line 187, in put_nowait
    return self.put(item, block=False)
  File "/Users/ruediger/data/p3/Python-3.4.2/Lib/queue.py", line 133, in put
    raise Full
queue.Full