An Intro to
Programming
with Python

Fall 2006
course
navigation

oct 2

ongoing business


we didn't get past looking at the homework. See the attachments below for bar.py and bar_input.txt that we created in class. - Jim


new stuff

some specifics about functions

def some_random_function(this, that, the_other): # do something here return some_thing answer = some_random_function(1, "hi", [3.95, 14.25, 1.98]) def double_a_value(x): x = 2*x # this changes x to double its value return x # and this sends it back y = 10 z = double_a_value(y) print y # what does this do? print z # how about this?
Make sure you understand exactly what's going on here.
def double_a_reference(the_list): the_list[0] = 2*the_list[0] return the_list y = [10] # this creates a list with a single element in it. z = double_a_reference(y) print y # what does this look like? print z # how about this?
Here's another way to see the difference. Try this in the interpreter
>>> a = 2 >>> b = a >>> a = 3 >>> b >>> a = [2] >>> b = a >>> a[0] = 3 >>> b >>> c = [3] >>> a,b,c # are these the same? >>> id(a), id(b), id(c) # what do you think now?
When you say "y=10", you're pointing a name (i.e. 'y') at a value (i.e. '10'). If you pass 'a' to a function, only the '10' (the value) gets there; the function has no idea what it's name is anywhere else. There's nothing in the function that will change the value of y in the outside environment. This is usually called "passing an argument by value".
On the other hand, when you say "y=[10]", you're pointing the name 'y' at a container, namely a list with one element. Its the container that gets passed to the function. If you change what's inside the container, that change can be seen outside the function - because outside the function, 'y' still points at the same container ... it just has different contents. This is usually called "passing an argument by reference" because the name refers to (points to) the data without actually being the data.
This is tricky stuff, which is handled slightly differently in different programming languages. But these underlying ideas can be found throughout programming circles, and understanding the difference is worth the effort.

more python tricks

def named_arg(x,y,z): point = PointInSpace(x,y,z) # something similar to graphics.py point.draw() named_args(1.0, 1.0, 3.0) # this works named_args(x=1.0, y=1.0, z=3.0) # so does this named_args(z=3.0, x=1.0, y=1.0) # or even this def default_args(x, y, z=10): point = PointInSpace(x,y,z) point.draw() default_args(1.0, 1.0) # z defaults to 10 def lots_of_args(a, b, *c) print "a = ",a print "b = ",b print "c = ",c lots_of_arg(1, 2, 3, 4, 5, 6) # Try it and see

dots 'n boxes

0 +---+---+ + possible -- -- f0 1 | O | X | moves => | | | g1 2 +---+---+ + -- -- f2 3 score a3 c3 e3 g3 4 + + + + O: 01 b4 d4 f4 5 X: 01 a5 c5 e5 g5 6 + + + + b6 d6 f6 a b c d e f g
... more to come.
http://cs.marlboro.edu/ courses/ fall2006/python/ notes/ oct_2
last modified Monday October 2 2006 11:22 am EDT

attachments [paper clip]

     name last modified size
   bars.py Oct 2 2006 11:21 am 1.43kB [TXT]bars_input.txt Oct 2 2006 11:21 am 47B