[Tutor] creating pop method for stack class

John Fouhy john at fouhy.net
Fri Jul 18 09:04:21 CEST 2008


On 18/07/2008, Christopher Spears <cspears2002 at yahoo.com> 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.


More information about the Tutor mailing list