an oop question

Giorgio Pastore pastgio at units.it
Sun Oct 30 16:06:00 EDT 2022


Il 30/10/22 15:54, Julieta Shem ha scritto:
> ram at zedat.fu-berlin.de (Stefan Ram) writes:
> 
>> Julieta Shem <jshem at yaxenu.org> writes:
>>> My desire seems to imply that I need a union-like data structure.
>>
>>    You only need to worry about such things in languages with
>>    static typing. For example, to have a function that can
>>    sometimes return an int value and at other times a string
>>    value in C, one would need a union.
>>
>>    In Python with its dynamic typing, one does not need unions.
>>
>> def an_int_sometimes_and_sometimes_a_string( x ):
>>      if x:
>>          return 2
>>      else:
>>          return "two"
> 
> Nice.  This means that I can solve my stack-union problem by writing a
> procedure --- say stack(...) --- that sometimes gives me Empty() and
> sometimes gives me Stack().
> 
I think that Stefan Ram's suggestion provides a solution to the problem 
of the stack, but not in a real OOP flavor.

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/

Giorgio



More information about the Python-list mailing list