Stack data structure in python

前端 未结 7 1449
轮回少年
轮回少年 2021-02-02 09:24

I have 2 issues with the code below:

  1. push(o) throws an exception TypeError: can only assign an iterable.
  2. Should I throw an exception if pop()

7条回答
  •  离开以前
    2021-02-02 09:50

    I won't talk about the list structure as that's already been covered in this question. Instead I'll mention my preferred method for dealing with stacks:

    I always use the Queue module. It supports FIFO and LIFO data structures and is thread safe.

    See the docs for more info. It doesn't implement a isEmpty() function, it instead raises a Full or Empty exception if a push or pop can't be done.

提交回复
热议问题