On 31/10/2022 09.22, Stefan Ram wrote:
Giorgio Pastore <past...@units.it> writes:
You may find useful to learn about the possibilities of using Python
tools to implement a stack data structure and its manipulation methods.
A good introduction is at
https://realpython.com/how-to-implement-python-stack/

   I can't see how lists are not stacks.

Agreed - in terms of functionality(!)

|>>> s=[]
|>>> s.append(1)
|>>> s.append(2)
|>>> s.pop()
|2
|>>> s.pop()
|1
|>>> s.pop()
|Traceback (most recent call last):
|  File "<stdin>", line 1, in <module>
|IndexError: pop from empty list

   So, for practical purposes, there is no need to implement a
   stack class as far as I can understand it.

   Maybe the OP wanted to implement a stack in a special way,
   so that this stack is immutable and its implementation is
   based on something like dotted pairs.


Code is for humans to read too. Accordingly, instead of "s", preference for "stack" - per OP terminology.

The OP has coded pop() and push() methods, which are familiar/taught as 'the' stack operations in every 'intro to algorithms' (or intermediate 'data-structures') class.

Sadly, whereas there is list.pop(), the equivalent "push" method is append(). Thus, a (small) dissonance in one's mind.

Writing a Stack class is unnecessary - at least given list, it is. However, having called the structure "stack" to be descriptive and helpful, it seems logical to continue by referring to the methods as "push" and "pop". (which is why many of us have such 'utilities'/snippets sitting-ready in a personal library)

The OP has already coded such, so no cost to keep.


(returning to the OP)
Like @Gerard, am confused. There are two types: Pair and Empty. Thereafter the need is to keep them on a stack. The stack is a collection, an aggregate of 'whatever'. Why inherit from/to Stack?
(is this a 'Lisp thing'?)

--
Regards,
=dn
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to