[Tutor] creating pop method for stack class
Martin Walsh
mwalsh at groktech.org
Fri Jul 18 15:32:04 CEST 2008
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
More information about the Tutor
mailing list