Re: [Tutor] creating pop method for stack class
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)) def peek(self): length = len(self) if length == 0: return 0 How do you know whether zero was the last item or an error? Better to raise an IndexError or a ValueError or define your own StackEmptyError. else: last_index = length - 1 return self[last_index] You don't need the last_index thing, just use -1 -1 is always the last item. def stackpop(self): length = len(self) if length == 0: print Empty list! And this is inconsistent with the previous method. Keep your error jhandling style the same or confuse your users. In general putting print statements inside class methods is a bad idea. Raise an exception instead. else: last_index = length - 1 stackpop_val = self[last_index] self = self[:last_index] return stackpop_val I don't think reassigning self is a good idea. I'd go with deleting the last member using del(self[-1]) My main problem seems to be getting this part to work: def stackpop(self): ... self = self[:last_index] return stackpop_val see comment above. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating pop method for stack class
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...why does it work outside of the function but not in it? [...] Huh, how do you explain that? Have a look at Alan's tutorial; in particular, the section on namespaces: http://www.freenetpages.co.uk/hp/alan.gauld/tutname.htm Or you might find this helpful also: http://www.greenteapress.com/thinkpython/html/book004.html#toc31 (sections 3.8 and 3.9) Also, try this, in a new interpreter session: Python 2.5.1 (r251:54869, Apr 18 2007, 22:08:04) [GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin Type help, copyright, credits or license for more information. def setXTo3(): ... x = 3 ... setXTo3() print x Is that the result you expect? If not, can you explain it after reading the web pages above? If it is what you expect, can you apply the same idea to the shorten(lst) function above? -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating pop method for stack class
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 outside of the function but not in it? Let me try something else: def shorten(lst): ... lst = lst[:-1] Perhaps it would be helpful to consider the following... def shorten1(lst): lst[:] = lst[:-1] ... or ... def shorten2(lst): lst.pop() Why might these exhibit different behavior? HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] creating pop method for stack class
I am almost done with a stack class that I wrote: #!/usr/bin/python class Stack(list): def isempty(self): length = len(self) if length == 0: return True else: return False def peek(self): length = len(self) if length == 0: return 0 else: last_index = length - 1 return self[last_index] def stackpop(self): length = len(self) if length == 0: print Empty list! else: last_index = length - 1 stackpop_val = self[last_index] self = self[:last_index] return stackpop_val def push(self, value): return self.append(value) if __name__ == '__main__': x = True stack = Stack() print Pick an option to modify stack: while x == True: print 1) Peek at the last value print 2) Pop off the last value print 3) Push a value on the stack print 4) Print stack print 5) Quit Program choice_string = raw_input(Make a choice: ) try: choice = int(choice_string) except ValueError: sys.exit(Not an integer! Goodbye!) if choice == 1: if stack.isempty(): print Stack is empty else: peek_val = stack.peek() print peek_val elif choice == 2: pop_val = stack.stackpop() print pop_val elif choice == 3: push_val = raw_input(Push this value on stack: ) stack.push(push_val) elif choice == 4: print stack elif choice == 5: print Goodbye! x = False else: x = False sys.exit(Wrong response Goodbye!) My main problem seems to be getting this part to work: def stackpop(self): length = len(self) if length == 0: print Empty list! else: last_index = length - 1 stackpop_val = self[last_index] self = self[:last_index] return stackpop_val The easiest solution would be to use the pop method from list, but I decided I wanted to take a crack at writing my own pop method. Unfortunately, this always happens when I run the program: 1) Peek at the last value 2) Pop off the last value 3) Push a value on the stack 4) Print stack 5) Quit Program Make a choice: 3 Push this value on stack: 1 1) Peek at the last value 2) Pop off the last value 3) Push a value on the stack 4) Print stack 5) Quit Program Make a choice: 3 Push this value on stack: blah 1) Peek at the last value 2) Pop off the last value 3) Push a value on the stack 4) Print stack 5) Quit Program Make a choice: 3 Push this value on stack: blah blah 1) Peek at the last value 2) Pop off the last value 3) Push a value on the stack 4) Print stack 5) Quit Program Make a choice: 2 blah blah 1) Peek at the last value 2) Pop off the last value 3) Push a value on the stack 4) Print stack 5) Quit Program Make a choice: 4 ['1', 'blah', 'blah blah'] 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] Any hints? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating pop method for stack class
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] shorten(lista) What do you think will be the result of: print lista ? 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 outside of the function but not in it? Let me try something else: def shorten(lst): ... lst = lst[:-1] ... return lst ... lista = [1,2,3,4] shorten(lista) [1, 2, 3] print lista [1, 2, 3, 4] lista = shorten(lista) print lista [1, 2, 3] Huh, how do you explain that? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor