Download presentation
Presentation is loading. Please wait.
1
Department of Computing Science
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. 2006/07 Computing Science 1P Lecture 16 - Simon Gay
3
Computing Science 1P Lecture 16 - Simon Gay
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 x+y >>> 5 2006/07 Computing Science 1P Lecture 16 - Simon Gay
4
Computing Science 1P Lecture 16 - Simon Gay
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 x+y >>> 5 2006/07 Computing Science 1P Lecture 16 - Simon Gay
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() 2006/07 Computing Science 1P Lecture 16 - Simon Gay
6
What does the program print?
2 then 1 1 then 2 2 then 2 1 then 1 Something else Don't know 2006/07 Computing Science 1P Lecture 16 - Simon Gay
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() these do not refer to the same "box" 2006/07 Computing Science 1P Lecture 16 - Simon Gay
8
Computing Science 1P Lecture 16 - Simon Gay
Stack diagrams (page 21) def test(): x = 1 print x x = 2 test() 2006/07 Computing Science 1P Lecture 16 - Simon Gay
9
Computing Science 1P Lecture 16 - Simon Gay
Stack diagrams (page 21) def test(): x = 1 print x x = 2 test() __main__ x 2 2006/07 Computing Science 1P Lecture 16 - Simon Gay
10
Computing Science 1P Lecture 16 - Simon Gay
Stack diagrams (page 21) def test(): x = 1 print x x = 2 test() __main__ x 2 test a new stack frame 2006/07 Computing Science 1P Lecture 16 - Simon Gay
11
Computing Science 1P Lecture 16 - Simon Gay
Stack diagrams (page 21) def test(): x = 1 print x x = 2 test() __main__ x 2 test x 1 2006/07 Computing Science 1P Lecture 16 - Simon Gay
12
Computing Science 1P Lecture 16 - Simon Gay
Stack diagrams (page 21) def test(): x = 1 print x x = 2 test() __main__ x 2 test x 1 2006/07 Computing Science 1P Lecture 16 - Simon Gay
13
Computing Science 1P Lecture 16 - Simon Gay
Stack diagrams (page 21) def test(): x = 1 print x x = 2 test() __main__ x 2 2006/07 Computing Science 1P Lecture 16 - Simon Gay
14
Computing Science 1P Lecture 16 - Simon Gay
Stack diagrams (page 21) def test(): x = 1 print x x = 2 test() __main__ x 2 2006/07 Computing Science 1P Lecture 16 - Simon Gay
15
Computing Science 1P Lecture 16 - Simon Gay
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 2006/07 Computing Science 1P Lecture 16 - Simon Gay
16
Stack diagram with function parameters
def calc(x): y = x + x return y x = 1 y = 2 z = calc(y) print z 2006/07 Computing Science 1P Lecture 16 - Simon Gay
17
Stack diagram with function parameters
def calc(x): y = x + x return y x = 1 y = 2 z = calc(y) print z __main__ x 1 2006/07 Computing Science 1P Lecture 16 - Simon Gay
18
Stack diagram with function parameters
def calc(x): y = x + x return y x = 1 y = 2 z = calc(y) print z __main__ x 1 y 2 2006/07 Computing Science 1P Lecture 16 - Simon Gay
19
Stack diagram with function parameters
def calc(x): y = x + x return y x = 1 y = 2 z = calc(y) print z __main__ x 1 y 2 2006/07 Computing Science 1P Lecture 16 - Simon Gay
20
Stack diagram with function parameters
def calc(x): y = x + x return y x = 1 y = 2 z = calc(y) print z __main__ x 1 y 2 2006/07 Computing Science 1P Lecture 16 - Simon Gay
21
Stack diagram with function parameters
def calc(x): y = x + x return y x = 1 y = 2 z = calc(y) print z __main__ x 1 y 2 calc x 2006/07 Computing Science 1P Lecture 16 - Simon Gay
22
Stack diagram with function parameters
def calc(x): y = x + x return y x = 1 y = 2 z = calc(y) print z __main__ x 1 y 2 calc x 2 2006/07 Computing Science 1P Lecture 16 - Simon Gay
23
Stack diagram with function parameters
def calc(x): y = x + x return y x = 1 y = 2 z = calc(y) print z __main__ x 1 y 2 calc x 2 y 2006/07 Computing Science 1P Lecture 16 - Simon Gay
24
Stack diagram with function parameters
def calc(x): y = x + x return y x = 1 y = 2 z = calc(y) print z __main__ x 1 y 2 calc x 2 y 4 2006/07 Computing Science 1P Lecture 16 - Simon Gay
25
Stack diagram with function parameters
def calc(x): y = x + x return y x = 1 y = 2 z = calc(y) print z __main__ x 1 y 2 calc x 2 y 4 2006/07 Computing Science 1P Lecture 16 - Simon Gay
26
Stack diagram with function parameters
def calc(x): y = x + x return y x = 1 y = 2 z = calc(y) print z __main__ x 1 y 2 calc x 2 y 4 2006/07 Computing Science 1P Lecture 16 - Simon Gay
27
Stack diagram with function parameters
def calc(x): y = x + x return y x = 1 y = 2 z = calc(y) print z __main__ x 1 y 2 z 4 2006/07 Computing Science 1P Lecture 16 - Simon Gay
28
Stack diagram with function parameters
def calc(x): y = x + x return y x = 1 y = 2 z = calc(y) print z __main__ x 1 y 2 z 4 2006/07 Computing Science 1P Lecture 16 - Simon Gay
29
Parameters really are local
Stack diagrams also allow us to see that assignments to the parameters, within a function, have no permanent effect. 2006/07 Computing Science 1P Lecture 16 - Simon Gay
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 2006/07 Computing Science 1P Lecture 16 - Simon Gay
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 __main__ x 1 2006/07 Computing Science 1P Lecture 16 - Simon Gay
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 __main__ x 1 y 2 2006/07 Computing Science 1P Lecture 16 - Simon Gay
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 __main__ x 1 y 2 2006/07 Computing Science 1P Lecture 16 - Simon Gay
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 __main__ x 1 y 2 2006/07 Computing Science 1P Lecture 16 - Simon Gay
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 __main__ x 1 y 2 calc x 2006/07 Computing Science 1P Lecture 16 - Simon Gay
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 __main__ x 1 y 2 calc x 2 2006/07 Computing Science 1P Lecture 16 - Simon Gay
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 __main__ x 1 y 2 calc x 2 y 2006/07 Computing Science 1P Lecture 16 - Simon Gay
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 __main__ x 1 y 2 calc x 2 y 4 2006/07 Computing Science 1P Lecture 16 - Simon Gay
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 __main__ x 1 y 2 calc x 2 y 4 2006/07 Computing Science 1P Lecture 16 - Simon Gay
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 __main__ x 1 y 2 calc x 5 y 4 2006/07 Computing Science 1P Lecture 16 - Simon Gay
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 __main__ x 1 y 2 calc x 5 y 4 2006/07 Computing Science 1P Lecture 16 - Simon Gay
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 __main__ x 1 y 2 calc x 5 y 4 2006/07 Computing Science 1P Lecture 16 - Simon Gay
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 __main__ x 1 y 2 z 4 2006/07 Computing Science 1P Lecture 16 - Simon Gay
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 __main__ x 1 y 2 z 4 2006/07 Computing Science 1P Lecture 16 - Simon Gay
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): x = [ 2 ] y = [ 1 ] test(y) print y def test(x): x[0] = 2 y = [ 1 ] test(y) print y Example 1 Example 2 2006/07 Computing Science 1P Lecture 16 - Simon Gay
46
What does Example 1 print?
def test(x): x = [ 2 ] y = [ 1 ] test(y) print y 2006/07 Computing Science 1P Lecture 16 - Simon Gay
47
Computing Science 1P Lecture 16 - Simon Gay
What does it print? [ 1 ] [ 2 ] Something else Don't know 2006/07 Computing Science 1P Lecture 16 - Simon Gay
48
What does Example 2 print?
def test(x): x[0] = 2 y = [ 1 ] test(y) print y 2006/07 Computing Science 1P Lecture 16 - Simon Gay
49
Computing Science 1P Lecture 16 - Simon Gay
What does it print? [ 1 ] [ 2 ] Something else Don't know 2006/07 Computing Science 1P Lecture 16 - Simon Gay
50
Computing Science 1P Lecture 16 - Simon Gay
Example 2 What happens if we try to understand this program in the way we have just seen? def test(x): x[0] = 2 y = [ 1 ] test(y) print y 2006/07 Computing Science 1P Lecture 16 - Simon Gay
51
Computing Science 1P Lecture 16 - Simon Gay
Example 2 __main__ y [ 1 ] def test(x): x[0] = 2 y = [ 1 ] test(y) print y 2006/07 Computing Science 1P Lecture 16 - Simon Gay
52
Computing Science 1P Lecture 16 - Simon Gay
Example 2 __main__ y [ 1 ] def test(x): x[0] = 2 y = [ 1 ] test(y) print y 2006/07 Computing Science 1P Lecture 16 - Simon Gay
53
Computing Science 1P Lecture 16 - Simon Gay
Example 2 __main__ y [ 1 ] def test(x): x[0] = 2 y = [ 1 ] test(y) print y test x [ 1 ] 2006/07 Computing Science 1P Lecture 16 - Simon Gay
54
Computing Science 1P Lecture 16 - Simon Gay
Example 2 __main__ y [ 1 ] def test(x): x[0] = 2 y = [ 1 ] test(y) print y test x [ 1 ] 2006/07 Computing Science 1P Lecture 16 - Simon Gay
55
Computing Science 1P Lecture 16 - Simon Gay
Example 2 __main__ y [ 1 ] def test(x): x[0] = 2 y = [ 1 ] test(y) print y test x [ 2 ] 2006/07 Computing Science 1P Lecture 16 - Simon Gay
56
Computing Science 1P Lecture 16 - Simon Gay
Example 2 __main__ y [ 1 ] def test(x): x[0] = 2 y = [ 1 ] test(y) print y But this is not what happens if we run the program! 2006/07 Computing Science 1P Lecture 16 - Simon Gay
57
Computing Science 1P Lecture 16 - Simon Gay
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: x [ 1, 2, 3 ] The value of x is a reference to an object which has an independent existence somewhere in the computer's memory. 2006/07 Computing Science 1P Lecture 16 - Simon Gay
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. 2006/07 Computing Science 1P Lecture 16 - Simon Gay
59
State diagrams with lists (1)
x = [ 1,2 ] x [ 1, 2 ] y = [ 3 ] x y [ 1, 2 ] [ 3 ] x = [ 4 ] [ 1, 2 ] x y [ 4 ] [ 3 ] 2006/07 Computing Science 1P Lecture 16 - Simon Gay
60
State diagrams with lists (1)
x = [ 1,2 ] x [ 1, 2 ] y = [ 3 ] x y [ 1, 2 ] [ 3 ] garbage x = [ 4 ] [ 1, 2 ] x y [ 4 ] [ 3 ] 2006/07 Computing Science 1P Lecture 16 - Simon Gay
61
State diagrams with lists (2)
x = [ 1,2 ] x [ 1, 2 ] x[0] = 3 x [ 3, 2 ] 2006/07 Computing Science 1P Lecture 16 - Simon Gay
62
Stack diagrams with lists
def test(): x = [ 1 ] print x x = [ 2 ] test() 2006/07 Computing Science 1P Lecture 16 - Simon Gay
63
Stack diagrams with lists
def test(): x = [ 1 ] print x x = [ 2 ] test() __main__ x [ 2 ] 2006/07 Computing Science 1P Lecture 16 - Simon Gay
64
Stack diagrams with lists
def test(): x = [ 1 ] print x x = [ 2 ] test() __main__ x [ 2 ] test a new stack frame 2006/07 Computing Science 1P Lecture 16 - Simon Gay
65
Stack diagrams with lists
def test(): x = [ 1 ] print x x = [ 2 ] test() __main__ x [ 2 ] test x [ 1 ] 2006/07 Computing Science 1P Lecture 16 - Simon Gay
66
Stack diagrams with lists
def test(): x = [ 1 ] print x x = [ 2 ] test() __main__ x [ 2 ] test x [ 1 ] 2006/07 Computing Science 1P Lecture 16 - Simon Gay
67
Stack diagrams with lists
def test(): x = [ 1 ] print x x = [ 2 ] test() __main__ x [ 2 ] 2006/07 Computing Science 1P Lecture 16 - Simon Gay
68
Stack diagrams with lists
def test(): x = [ 1 ] print x x = [ 2 ] test() __main__ x [ 2 ] 2006/07 Computing Science 1P Lecture 16 - Simon Gay
69
Stack diagram: Example 1
def test(x): x = [ 2 ] y = [ 1 ] test(y) print y >>> [ 1 ] 2006/07 Computing Science 1P Lecture 16 - Simon Gay
70
Stack diagram: Example 1
def test(x): x = [ 2 ] y = [ 1 ] test(y) print y >>> [ 1 ] __main__ y [ 1 ] 2006/07 Computing Science 1P Lecture 16 - Simon Gay
71
Stack diagram: Example 1
def test(x): x = [ 2 ] y = [ 1 ] test(y) print y >>> [ 1 ] __main__ y [ 1 ] 2006/07 Computing Science 1P Lecture 16 - Simon Gay
72
Stack diagram: Example 1
def test(x): x = [ 2 ] y = [ 1 ] test(y) print y >>> [ 1 ] __main__ y [ 1 ] test x 2006/07 Computing Science 1P Lecture 16 - Simon Gay
73
Stack diagram: Example 1
def test(x): x = [ 2 ] y = [ 1 ] test(y) print y >>> [ 1 ] __main__ y [ 1 ] test x [ 2 ] 2006/07 Computing Science 1P Lecture 16 - Simon Gay
74
Stack diagram: Example 1
def test(x): x = [ 2 ] y = [ 1 ] test(y) print y >>> [ 1 ] __main__ y [ 1 ] 2006/07 Computing Science 1P Lecture 16 - Simon Gay
75
Stack diagram: Example 2
def test(x): x[0] = 2 y = [ 1 ] test(y) print y >>> [ 2 ] 2006/07 Computing Science 1P Lecture 16 - Simon Gay
76
Stack diagram: Example 2
def test(x): x[0] = 2 y = [ 1 ] test(y) print y >>> [ 2 ] __main__ y [ 1 ] 2006/07 Computing Science 1P Lecture 16 - Simon Gay
77
Stack diagram: Example 2
def test(x): x[0] = 2 y = [ 1 ] test(y) print y >>> [ 2 ] __main__ y [ 1 ] 2006/07 Computing Science 1P Lecture 16 - Simon Gay
78
Stack diagram: Example 2
def test(x): x[0] = 2 y = [ 1 ] test(y) print y >>> [ 2 ] __main__ y [ 1 ] test x 2006/07 Computing Science 1P Lecture 16 - Simon Gay
79
Stack diagram: Example 2
def test(x): x[0] = 2 y = [ 1 ] test(y) print y >>> [ 2 ] __main__ y [ 2 ] test x 2006/07 Computing Science 1P Lecture 16 - Simon Gay
80
Stack diagram: Example 2
def test(x): x[0] = 2 y = [ 1 ] test(y) print y >>> [ 2 ] __main__ y [ 2 ] 2006/07 Computing Science 1P Lecture 16 - Simon Gay
81
Computing Science 1P Lecture 16 - Simon Gay
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 ] x[0] = 5 y 2006/07 Computing Science 1P Lecture 16 - Simon Gay
82
Computing Science 1P Lecture 16 - Simon Gay
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 ] x[0] = 5 y [ 1,2,3 ] 2006/07 Computing Science 1P Lecture 16 - Simon Gay
83
Computing Science 1P Lecture 16 - Simon Gay
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. 2006/07 Computing Science 1P Lecture 16 - Simon Gay
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.