[Tutor] meaning

Bruce Sass bsass@freenet.edmonton.ab.ca
Mon, 7 May 2001 17:58:06 -0600 (MDT)


On Mon, 7 May 2001, Cameron wrote:

> Hi all,
>
> Would someone be so kind as to explain what this bit of code means line by line.
> Mostly I would like to know what the first four lines mean.
> Thanks,
> -Cameron
>
> #!/usr/local/bin/python

Tells the shell to hand the file off to the python interpreter at
/usr/local/bin/python, probably a hardlink to python1.5|2.0|2.1

> def spam(n, l =[] ) :

Defines a function named "spam" with two parameters, one is required
the other defaults to being an empty list the first time the function
is used.  This is what you usually want...

	def spam(n, l = None):
	    if l == None:
	        l = []

This way you create a new list object each time you don't pass a
second argument, instead of creating one only when the function gets
compiled.

>     l.append(n)

appends n to l; n can be anything, l can be anything with an
"append" method

>     return l

returns the object "l" as the result

> x = spam(42)
> print x
> y = spam(39)
> print y
> z = spam(9999, y)
> print x, y, z

print id(x), id(y), id(z)


- Bruce