oct 2
ongoing business
- discuss / answer questions about graphics.py homework due today
- mention <code> tag in wiki
- point towards assignment for next Mon
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
- start chapter 8 - functions :
- one of the key notions in all of programming
- breaks problems into smaller chunks
- well-defined inputs to well-defined outputs
- testable
- reusable
some specifics about functions
- why do we need them - discuss
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])
- 'return value'
- 'binding' of names and values
- pass by value
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
- (whether we do this will depend on time; probably we'll set it later this week)
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
- example 'larger project' : dots-n-boxes
- Here's a text-only example of a board position. The idea is that a player could type something like 'a3' as a move, and then the program would print out the new board.
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
- I'm thinking I'll work on this myself during the term, and have a program that plays the game by the end of the semester as an example of "real programming".
- For now, I just bring it up as an example of a program that's more than big enough to need breaking into smaller pieces.
- What do you think some of the functions would do?
- How could we store the data?
- If we wanted a graphical version with graphics.py, could some parts of the program be recyled?
- Should it work for people vs people? people vs computer? computer vs computer?
- What sorts of things would a computer program that plays the game need to do?
- Could we write a 'random move' computer version as a test?
- How could 'objects' fit into all this?
... more to come.