Re: [Tutor] creating pop method for stack class

2008-07-18 Thread Martin Walsh
Christopher Spears wrote: I see what you mean. I have tested it, and I have gotten a weird result: def shorten(lst): ... lst = lst[:-1] ... lista = [1,2,3,4] shorten(lista) print lista [1, 2, 3, 4] lista = [1,2,3,4] lista = lista[:-1] print lista [1, 2, 3] Strange...why does it work o

Re: [Tutor] creating pop method for stack class

2008-07-18 Thread John Fouhy
On 18/07/2008, Christopher Spears <[EMAIL PROTECTED]> wrote: > I see what you mean. I have tested it, and I have gotten a weird result: > > >>> def shorten(lst): > ... lst = lst[:-1] > > ... > > >>> lista = [1,2,3,4] > >>> shorten(lista) > >>> print lista > [1, 2, 3, 4] [...] > Strange...wh

Re: [Tutor] creating pop method for stack class

2008-07-17 Thread Alan Gauld
"Christopher Spears" <[EMAIL PROTECTED]> wrote I am almost done with a stack class that I wrote: Some comments... class Stack(list): def isempty(self): length = len(self) if length == 0: return True else: return False This can just be return bool(len(self)

Re: [Tutor] creating pop method for stack class

2008-07-17 Thread Christopher Spears
> > First, a tip: > > Instead of lista[:len(lista)-1], you can (and should) just > write lista[:-1]. > > Now, what if we wrap that in a function: > > >>> def shorten(lst): > ... lst = lst[:-1] # identical to: lst = > lst[:len(lst)-1] > ... > > Then test it: > > >>> lista = [1, 2, 3, 4] >

Re: [Tutor] creating pop method for stack class

2008-07-17 Thread John Fouhy
On 18/07/2008, Christopher Spears <[EMAIL PROTECTED]> wrote: > How come the stack doesn't shrink when I pop off the last value? I tested > the code in the interpreter: > > >>> lista = [1,2,3,4] > >>> lista[:len(lista)-1] > [1, 2, 3] > >>> lista = lista[:len(lista)-1] > >>> lista > [1, 2, 3