an oop question

Julieta Shem jshem at yaxenu.org
Tue Nov 1 16:54:37 EDT 2022


ram at zedat.fu-berlin.de (Stefan Ram) writes:

> Julieta Shem <jshem at yaxenu.org> writes:
>>clarify.  If I wish for an empty stack, I wish I could just say
>>>>> Stack()
>>Stack()
>>and if I wish for a nonempty stack, I'd write
>>>>> Stack(1, Stack(2, Stack(3, Stack())))
>>Stack(1, Stack(2, Stack(3, Stack())))
>
>   If this is all,
>
>   main.py
>
> class Stack:
>     def __init__( self, *args ):
>         self.data = [ args[ 0 ], args[ 1 ]]if len( args ) else []
>     def __str__( self ):
>         if len( self.data ):
>             return f"Stack({self.data[0]}, {self.data[1]})"
>         else:
>             return f"Stack()"
>
> print( Stack() )
>
> print( Stack(1, Stack(2, Stack(3, Stack()))) )
>
>   output
>
> Stack()
> Stack(1, Stack(2, Stack(3, Stack())))

Thanks!  But we've left behind a more basic requirement --- the Stack
class wishes for all the methods already written in some class called
Pair, which has a certain connection with a class Empty.  This
requirement has been implicit in the first message in this thread,
though.  I didn't make it explicit.  (In other words, this part of the
thread only makes sense by taking into account all the previous messages
up in the thread.)


More information about the Python-list mailing list