Computing Science 1P - PowerPoint PPT Presentation

About This Presentation
Title:

Computing Science 1P

Description:

and programming techniques, and try to gain a more detailed. understanding of the way in which programs are executed. ... skimmed over or ignored up to now. ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 84
Provided by: simo7
Category:

less

Transcript and Presenter's Notes

Title: Computing Science 1P


1
Computing Science 1P
Lecture 16 Friday 16th February
Simon Gay Department of Computing
Science University of Glasgow
2006/07
2
Variables, 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.
3
Variables
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
4
Variables
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
5
What 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
6
What does the program print?
  • 2 then 1
  • 1 then 2
  • 2 then 2
  • 1 then 1
  • Something else
  • Don't know

7
What 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"
8
Stack diagrams (page 21)
def test() x 1 print x x 2 test() print x
9
Stack diagrams (page 21)
def test() x 1 print x x 2 test() print x
x ? 2
__main__
10
Stack diagrams (page 21)
def test() x 1 print x x 2 test() print x
x ? 2
__main__

test
a new stack frame
11
Stack diagrams (page 21)
def test() x 1 print x x 2 test() print x
x ? 2
__main__
x ? 1
test
12
Stack diagrams (page 21)
def test() x 1 print x x 2 test() print x
x ? 2
__main__
x ? 1
test
13
Stack diagrams (page 21)
def test() x 1 print x x 2 test() print x
x ? 2
__main__
14
Stack diagrams (page 21)
def test() x 1 print x x 2 test() print x
x ? 2
__main__
15
What 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
16
Stack diagram with function parameters
def calc(x) y x x return y x 1 y 2 z
calc(y) print z
17
Stack diagram with function parameters
def calc(x) y x x return y x 1 y 2 z
calc(y) print z
x ? 1
__main__
18
Stack 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__
19
Stack 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__
20
Stack 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__
21
Stack 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 ?
22
Stack 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
23
Stack 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 ?
24
Stack 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
25
Stack 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
26
Stack 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
27
Stack 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__
28
Stack 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__
29
Parameters really are local
Stack diagrams also allow us to see that
assignments to the parameters, within a function,
have no permanent effect.
30
Stack diagram with function parameters
def calc(x) y x x x 5 return y x 1 y
2 z calc(y) print z
31
Stack 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__
32
Stack 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__
33
Stack 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__
34
Stack 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__
35
Stack 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 ?
36
Stack 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
37
Stack 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 ?
38
Stack 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
39
Stack 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
40
Stack 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
41
Stack 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
42
Stack 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
43
Stack 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__
44
Stack 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__
45
Objects 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
46
What does Example 1 print?
def test(x) x 2 y 1 test(y) print y
47
What does it print?
  • 1
  • 2
  • Something else
  • Don't know

48
What does Example 2 print?
def test(x) x0 2 y 1 test(y) print y
49
What does it print?
  • 1
  • 2
  • Something else
  • Don't know

50
Example 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
51
Example 2
y ? 1
__main__
def test(x) x0 2 y 1 test(y) print y
52
Example 2
y ? 1
__main__
def test(x) x0 2 y 1 test(y) print y
53
Example 2
y ? 1
__main__
def test(x) x0 2 y 1 test(y) print y
test
x ? 1
54
Example 2
y ? 1
__main__
def test(x) x0 2 y 1 test(y) print y
test
x ? 1
55
Example 2
y ? 1
__main__
def test(x) x0 2 y 1 test(y) print y
test
x ? 2
56
Example 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!
57
Objects
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.
58
State diagrams with lists
Now we can look at our previous examples, with
lists instead of simple values, to see how our
new picture looks.
59
State 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
60
State 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
61
State diagrams with lists (2)
x 1,2
x ?
1, 2
x0 3
x ?
3, 2
62
Stack diagrams with lists
def test() x 1 print x x 2
test() print x
63
Stack diagrams with lists
def test() x 1 print x x 2
test() print x
x ?
__main__
2
64
Stack diagrams with lists
def test() x 1 print x x 2
test() print x
x ?
__main__
2

test
a new stack frame
65
Stack diagrams with lists
def test() x 1 print x x 2
test() print x
x ?
__main__
2
x ?
1
test
66
Stack diagrams with lists
def test() x 1 print x x 2
test() print x
x ?
__main__
2
x ?
1
test
67
Stack diagrams with lists
def test() x 1 print x x 2
test() print x
x ?
__main__
2
68
Stack diagrams with lists
def test() x 1 print x x 2
test() print x
x ?
__main__
2
69
Stack diagram Example 1
def test(x) x 2 y 1 test(y) print
y gtgtgt 1
70
Stack diagram Example 1
def test(x) x 2 y 1 test(y) print
y gtgtgt 1
y ?
__main__
1
71
Stack diagram Example 1
def test(x) x 2 y 1 test(y) print
y gtgtgt 1
y ?
__main__
1
72
Stack diagram Example 1
def test(x) x 2 y 1 test(y) print
y gtgtgt 1
y ?
__main__
1
test
x ?
73
Stack diagram Example 1
def test(x) x 2 y 1 test(y) print
y gtgtgt 1
y ?
__main__
1
test
x ?
2
74
Stack diagram Example 1
def test(x) x 2 y 1 test(y) print
y gtgtgt 1
y ?
__main__
1
75
Stack diagram Example 2
def test(x) x0 2 y 1 test(y) print
y gtgtgt 2
76
Stack diagram Example 2
def test(x) x0 2 y 1 test(y) print
y gtgtgt 2
y ?
__main__
1
77
Stack diagram Example 2
def test(x) x0 2 y 1 test(y) print
y gtgtgt 2
y ?
__main__
1
78
Stack diagram Example 2
def test(x) x0 2 y 1 test(y) print
y gtgtgt 2
y ?
__main__
1
test
x ?
79
Stack diagram Example 2
def test(x) x0 2 y 1 test(y) print
y gtgtgt 2
y ?
__main__
2
test
x ?
80
Stack diagram Example 2
def test(x) x0 2 y 1 test(y) print
y gtgtgt 2
y ?
__main__
2
81
Aliasing (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 ?
82
Copying 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
83
There 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.
Write a Comment
User Comments (0)
About PowerShow.com