Title: Computing Science 1P
1Computing Science 1P
Lecture 16 Friday 16th February
Simon Gay Department of Computing
Science University of Glasgow
2006/07
2Variables, objects, parameters, functions
We are going to pause in our introduction of
Python features and programming techniques, and
try to gain a more detailed understanding of the
way in which programs are executed.
We will return to several points in the book
which we have skimmed over or ignored up to now.
Understanding these points is essential for a
thorough mastery of programming.
3Variables
The most basic way of understanding a variable is
that it is a box in which a value is stored.
x
x 2
2
y 3
x
y
2
3
print xy gtgtgt 5
4Variables
The most basic way of understanding a variable is
that it is a box in which a value is stored.
x 2
x ? 2
state diagrams page 8
y 3
x ? 2 y ? 3
print xy gtgtgt 5
5What about local variables?
Simple state diagrams are not adequate to explain
the behaviour of the following program
def test() x 1 print x x 2 test() print x
6What does the program print?
- 2 then 1
- 1 then 2
- 2 then 2
- 1 then 1
- Something else
- Don't know
7What about local variables?
Simple state diagrams are not adequate to explain
the behaviour of the following program
def test() x 1 print x x 2 test() print x
these do not refer to the same "box"
8Stack diagrams (page 21)
def test() x 1 print x x 2 test() print x
9Stack diagrams (page 21)
def test() x 1 print x x 2 test() print x
x ? 2
__main__
10Stack diagrams (page 21)
def test() x 1 print x x 2 test() print x
x ? 2
__main__
test
a new stack frame
11Stack diagrams (page 21)
def test() x 1 print x x 2 test() print x
x ? 2
__main__
x ? 1
test
12Stack diagrams (page 21)
def test() x 1 print x x 2 test() print x
x ? 2
__main__
x ? 1
test
13Stack diagrams (page 21)
def test() x 1 print x x 2 test() print x
x ? 2
__main__
14Stack diagrams (page 21)
def test() x 1 print x x 2 test() print x
x ? 2
__main__
15What about parameters?
A function's parameters are similar to local
variables, whose initial values are the parameter
values supplied by the function call.
def calc(x) y x x return y x 1 y 2 z
calc(y) print z
16Stack diagram with function parameters
def calc(x) y x x return y x 1 y 2 z
calc(y) print z
17Stack diagram with function parameters
def calc(x) y x x return y x 1 y 2 z
calc(y) print z
x ? 1
__main__
18Stack diagram with function parameters
def calc(x) y x x return y x 1 y 2 z
calc(y) print z
x ? 1 y ? 2
__main__
19Stack diagram with function parameters
def calc(x) y x x return y x 1 y 2 z
calc(y) print z
x ? 1 y ? 2
__main__
20Stack diagram with function parameters
def calc(x) y x x return y x 1 y 2 z
calc(y) print z
x ? 1 y ? 2
__main__
21Stack diagram with function parameters
def calc(x) y x x return y x 1 y 2 z
calc(y) print z
x ? 1 y ? 2
__main__
calc
x ?
22Stack diagram with function parameters
def calc(x) y x x return y x 1 y 2 z
calc(y) print z
x ? 1 y ? 2
__main__
calc
x ? 2
23Stack diagram with function parameters
def calc(x) y x x return y x 1 y 2 z
calc(y) print z
x ? 1 y ? 2
__main__
calc
x ? 2 y ?
24Stack diagram with function parameters
def calc(x) y x x return y x 1 y 2 z
calc(y) print z
x ? 1 y ? 2
__main__
calc
x ? 2 y ? 4
25Stack diagram with function parameters
def calc(x) y x x return y x 1 y 2 z
calc(y) print z
x ? 1 y ? 2
__main__
calc
x ? 2 y ? 4
26Stack diagram with function parameters
def calc(x) y x x return y x 1 y 2 z
calc(y) print z
x ? 1 y ? 2
__main__
calc
x ? 2 y ? 4
27Stack diagram with function parameters
def calc(x) y x x return y x 1 y 2 z
calc(y) print z
x ? 1 y ? 2 z ? 4
__main__
28Stack diagram with function parameters
def calc(x) y x x return y x 1 y 2 z
calc(y) print z
x ? 1 y ? 2 z ? 4
__main__
29Parameters really are local
Stack diagrams also allow us to see that
assignments to the parameters, within a function,
have no permanent effect.
30Stack diagram with function parameters
def calc(x) y x x x 5 return y x 1 y
2 z calc(y) print z
31Stack diagram with function parameters
def calc(x) y x x x 5 return y x 1 y
2 z calc(y) print z
x ? 1
__main__
32Stack diagram with function parameters
def calc(x) y x x x 5 return y x 1 y
2 z calc(y) print z
x ? 1 y ? 2
__main__
33Stack diagram with function parameters
def calc(x) y x x x 5 return y x 1 y
2 z calc(y) print z
x ? 1 y ? 2
__main__
34Stack diagram with function parameters
def calc(x) y x x x 5 return y x 1 y
2 z calc(y) print z
x ? 1 y ? 2
__main__
35Stack diagram with function parameters
def calc(x) y x x x 5 return y x 1 y
2 z calc(y) print z
x ? 1 y ? 2
__main__
calc
x ?
36Stack diagram with function parameters
def calc(x) y x x x 5 return y x 1 y
2 z calc(y) print z
x ? 1 y ? 2
__main__
calc
x ? 2
37Stack diagram with function parameters
def calc(x) y x x x 5 return y x 1 y
2 z calc(y) print z
x ? 1 y ? 2
__main__
calc
x ? 2 y ?
38Stack diagram with function parameters
def calc(x) y x x x 5 return y x 1 y
2 z calc(y) print z
x ? 1 y ? 2
__main__
calc
x ? 2 y ? 4
39Stack diagram with function parameters
def calc(x) y x x x 5 return y x 1 y
2 z calc(y) print z
x ? 1 y ? 2
__main__
calc
x ? 2 y ? 4
40Stack diagram with function parameters
def calc(x) y x x x 5 return y x 1 y
2 z calc(y) print z
x ? 1 y ? 2
__main__
calc
x ? 5 y ? 4
41Stack diagram with function parameters
def calc(x) y x x x 5 return y x 1 y
2 z calc(y) print z
x ? 1 y ? 2
__main__
calc
x ? 5 y ? 4
42Stack diagram with function parameters
def calc(x) y x x x 5 return y x 1 y
2 z calc(y) print z
x ? 1 y ? 2
__main__
calc
x ? 5 y ? 4
43Stack diagram with function parameters
def calc(x) y x x x 5 return y x 1 y
2 z calc(y) print z
x ? 1 y ? 2 z ? 4
__main__
44Stack diagram with function parameters
def calc(x) y x x x 5 return y x 1 y
2 z calc(y) print z
x ? 1 y ? 2 z ? 4
__main__
45Objects behave differently
We have already mentioned that lists and
dictionaries are known as objects. The state
diagrams and stack diagrams we have used so far
are not adequate to explain the behaviour
of objects.
def test(x) x0 2 y 1 test(y) print y
def test(x) x 2 y 1 test(y) print y
Example 2
Example 1
46What does Example 1 print?
def test(x) x 2 y 1 test(y) print y
47What does it print?
- 1
- 2
- Something else
- Don't know
48What does Example 2 print?
def test(x) x0 2 y 1 test(y) print y
49What does it print?
- 1
- 2
- Something else
- Don't know
50Example 2
What happens if we try to understand this program
in the way we have just seen?
def test(x) x0 2 y 1 test(y) print y
51Example 2
y ? 1
__main__
def test(x) x0 2 y 1 test(y) print y
52Example 2
y ? 1
__main__
def test(x) x0 2 y 1 test(y) print y
53Example 2
y ? 1
__main__
def test(x) x0 2 y 1 test(y) print y
test
x ? 1
54Example 2
y ? 1
__main__
def test(x) x0 2 y 1 test(y) print y
test
x ? 1
55Example 2
y ? 1
__main__
def test(x) x0 2 y 1 test(y) print y
test
x ? 2
56Example 2
y ? 1
__main__
def test(x) x0 2 y 1 test(y) print y
But this is not what happens if we run the
program!
57Objects
We have to think of objects (including lists and
dictionaries) in a different way.
To represent the state arising from x 1,2,3
instead of a state diagram like this
x ? 1,2,3
use
1, 2, 3
x ?
The value of x is a reference to an object which
has an independent existence somewhere in the
computer's memory.
58State diagrams with lists
Now we can look at our previous examples, with
lists instead of simple values, to see how our
new picture looks.
59State diagrams with lists (1)
x 1,2
x ?
1, 2
y 3
x ? y ?
1, 2
3
1, 2
x 4
x ? y ?
4
3
60State diagrams with lists (1)
x 1,2
x ?
1, 2
y 3
x ? y ?
1, 2
3
garbage
1, 2
x 4
x ? y ?
4
3
61State diagrams with lists (2)
x 1,2
x ?
1, 2
x0 3
x ?
3, 2
62Stack diagrams with lists
def test() x 1 print x x 2
test() print x
63Stack diagrams with lists
def test() x 1 print x x 2
test() print x
x ?
__main__
2
64Stack diagrams with lists
def test() x 1 print x x 2
test() print x
x ?
__main__
2
test
a new stack frame
65Stack diagrams with lists
def test() x 1 print x x 2
test() print x
x ?
__main__
2
x ?
1
test
66Stack diagrams with lists
def test() x 1 print x x 2
test() print x
x ?
__main__
2
x ?
1
test
67Stack diagrams with lists
def test() x 1 print x x 2
test() print x
x ?
__main__
2
68Stack diagrams with lists
def test() x 1 print x x 2
test() print x
x ?
__main__
2
69Stack diagram Example 1
def test(x) x 2 y 1 test(y) print
y gtgtgt 1
70Stack diagram Example 1
def test(x) x 2 y 1 test(y) print
y gtgtgt 1
y ?
__main__
1
71Stack diagram Example 1
def test(x) x 2 y 1 test(y) print
y gtgtgt 1
y ?
__main__
1
72Stack diagram Example 1
def test(x) x 2 y 1 test(y) print
y gtgtgt 1
y ?
__main__
1
test
x ?
73Stack diagram Example 1
def test(x) x 2 y 1 test(y) print
y gtgtgt 1
y ?
__main__
1
test
x ?
2
74Stack diagram Example 1
def test(x) x 2 y 1 test(y) print
y gtgtgt 1
y ?
__main__
1
75Stack diagram Example 2
def test(x) x0 2 y 1 test(y) print
y gtgtgt 2
76Stack diagram Example 2
def test(x) x0 2 y 1 test(y) print
y gtgtgt 2
y ?
__main__
1
77Stack diagram Example 2
def test(x) x0 2 y 1 test(y) print
y gtgtgt 2
y ?
__main__
1
78Stack diagram Example 2
def test(x) x0 2 y 1 test(y) print
y gtgtgt 2
y ?
__main__
1
test
x ?
79Stack diagram Example 2
def test(x) x0 2 y 1 test(y) print
y gtgtgt 2
y ?
__main__
2
test
x ?
80Stack diagram Example 2
def test(x) x0 2 y 1 test(y) print
y gtgtgt 2
y ?
__main__
2
81Aliasing (page 60)
The previous example also illustrated that it is
possible to have two (or more) references to the
same object.
x 1,2,3
x ?
1,2,3
y x
x ?
1,2,3
x and y are aliases for the same list
y ?
x ?
5,2,3
x0 5
y ?
82Copying lists
What if we really want to make a copy of a list?
x 1,2,3
x ?
1,2,3
y x
x ?
1,2,3
y ?
1,2,3
x ?
5,2,3
x0 5
y ?
1,2,3
83There is more to say
This is not the full story yet, and we will
return to it later.
If you want to know more, look up shallow copying
and deep copying.
Also, you could find out what the append method
of a list does.