38
Python (v3) Stack Frame Examples CS21 at Swarthmore College

Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

  • Upload
    others

  • View
    10

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Python (v3) Stack Frame Examples

CS21 at Swarthmore College

Page 2: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Basic Example

1 def f(x,y):2 x = x + y3 print(x)4 return x5

6 def main():7 n = 48 out = f(n,2)9 print(out)

10

11 main()

f:2

x

y

main:

At the beginning of the program, main is called. We createa new stack frame. Since main has no parameters, the stackframe is empty.

2/3

Page 3: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Basic Example

1 def f(x,y):2 x = x + y3 print(x)4 return x5

6 def main():7 n = 48 out = f(n,2)9 print(out)

10

11 main()

f:2

x

y

main:7

At the beginning of the program, main is called. We createa new stack frame. Since main has no parameters, the stackframe is empty.

2/3

Page 4: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Basic Example

1 def f(x,y):2 x = x + y3 print(x)4 return x5

6 def main():7 n = 48 out = f(n,2)9 print(out)

10

11 main()

f:2

x

y

main:7

When line 7 of main is executed, the variable n is set to thevalue 4. We symbolize this by writing the variable name in thestack frame and creating an object on the heap for the value.We draw an arrow from the variable to its value.

2/3

Page 5: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Basic Example

1 def f(x,y):2 x = x + y3 print(x)4 return x5

6 def main():7 n = 48 out = f(n,2)9 print(out)

10

11 main()

f:2

x

y

main:8

n 4

When line 7 of main is executed, the variable n is set to thevalue 4. We symbolize this by writing the variable name in thestack frame and creating an object on the heap for the value.We draw an arrow from the variable to its value.

2/3

Page 6: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Basic Example

1 def f(x,y):2 x = x + y3 print(x)4 return x5

6 def main():7 n = 48 out = f(n,2)9 print(out)

10

11 main()

f:2

x

y

main:8

n 4

When line 8 is executed, we will call f. To do so, we mustfirst determine the value of each of its arguments. In thiscase, the first parameter is n, whose value is currently 4. Thesecond parameter is just 2.

2/3

Page 7: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Basic Example

1 def f(x,y):2 x = x + y3 print(x)4 return x5

6 def main():7 n = 48 out = f(n,2)9 print(out)

10

11 main()

f:2

x

y

main:8

n 4

Once we’ve established the value of the arguments on line 8(4 and 2, respectively), the f function is called. We createa new stack frame. Since f has two parameters, we createvariables for them in the stack frame. They point to theircorresponding values on the heap. 2/3

Page 8: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Basic Example

1 def f(x,y):2 x = x + y3 print(x)4 return x5

6 def main():7 n = 48 out = f(n,2)9 print(out)

10

11 main()

f:2

x

y 2

main:8

n 4

Once we’ve established the value of the arguments on line 8(4 and 2, respectively), the f function is called. We createa new stack frame. Since f has two parameters, we createvariables for them in the stack frame. They point to theircorresponding values on the heap. 2/3

Page 9: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Basic Example

1 def f(x,y):2 x = x + y3 print(x)4 return x5

6 def main():7 n = 48 out = f(n,2)9 print(out)

10

11 main()

f:2

x

y 2

main:8

n 4

Note that the stack frame for main is keeping track of wherewe were in that function. When we are done with f, we willreturn to that line.

2/3

Page 10: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Basic Example

1 def f(x,y):2 x = x + y3 print(x)4 return x5

6 def main():7 n = 48 out = f(n,2)9 print(out)

10

11 main()

f:2

x

y 2

main:8

n 4

When we run line 2 in f, we will update the variable x byadding the contents of the variable y to it. Since ints areimmutable, we will create a new object on the heap and makex point to it.

2/3

Page 11: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Basic Example

1 def f(x,y):2 x = x + y3 print(x)4 return x5

6 def main():7 n = 48 out = f(n,2)9 print(out)

10

11 main()

f:3

x

y 2

6

main:8

n 4

When we run line 2 in f, we will update the variable x byadding the contents of the variable y to it. Since ints areimmutable, we will create a new object on the heap and makex point to it.

2/3

Page 12: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Basic Example

1 def f(x,y):2 x = x + y3 print(x)4 return x5

6 def main():7 n = 48 out = f(n,2)9 print(out)

10

11 main()

f:3

x

y 2

6

main:8

n 4

Line 3 will print the contents of the x variable: in this case,6.

2/3

Page 13: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Basic Example

1 def f(x,y):2 x = x + y3 print(x)4 return x5

6 def main():7 n = 48 out = f(n,2)9 print(out)

10

11 main()

f:4

x

y 2

6

main:8

n 4

Line 3 will print the contents of the x variable: in this case,6.

2/3

Page 14: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Basic Example

1 def f(x,y):2 x = x + y3 print(x)4 return x5

6 def main():7 n = 48 out = f(n,2)9 print(out)

10

11 main()

f:4

x

y 2

6

main:8

n 4

Line 4 will return the value of x to the place where f wascalled. As a result, the variable out in main is given the value6, and the frame for f will be removed from the stack.

2/3

Page 15: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Basic Example

1 def f(x,y):2 x = x + y3 print(x)4 return x5

6 def main():7 n = 48 out = f(n,2)9 print(out)

10

11 main()

f:4

x

y

2

6

main:8

n 4

Line 4 will return the value of x to the place where f wascalled. As a result, the variable out in main is given the value6, and the frame for f will be removed from the stack.

2/3

Page 16: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Basic Example

1 def f(x,y):2 x = x + y3 print(x)4 return x5

6 def main():7 n = 48 out = f(n,2)9 print(out)

10

11 main()

f:4

x

y

2

6

main:9

n

out

4

Line 4 will return the value of x to the place where f wascalled. As a result, the variable out in main is given the value6, and the frame for f will be removed from the stack.

2/3

Page 17: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Basic Example

1 def f(x,y):2 x = x + y3 print(x)4 return x5

6 def main():7 n = 48 out = f(n,2)9 print(out)

10

11 main()

f:4

x

y

2

6

main:9

n

out

4

Line 9 prints the contents of the out variable (here, 6). Afterit runs, the main function is complete and the program isfinished.

2/3

Page 18: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Basic Example

1 def f(x,y):2 x = x + y3 print(x)4 return x5

6 def main():7 n = 48 out = f(n,2)9 print(out)

10

11 main()

f:4

x

y

2

6

main:

n

out

4

Line 9 prints the contents of the out variable (here, 6). Afterit runs, the main function is complete and the program isfinished.

2/3

Page 19: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Lists Example

1 def add_twice(x,lst):2 lst.append(x)3 lst.append(x)4

5 def main():6 data = [1]7 add_twice(2,data)8 print(data)9 add_twice(3,data)

10 print(data)11

12 main()

f:

x

lst

main:

1

list

As before, main is called at the start of this program. We createa new stack frame for it.

3/3

Page 20: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Lists Example

1 def add_twice(x,lst):2 lst.append(x)3 lst.append(x)4

5 def main():6 data = [1]7 add_twice(2,data)8 print(data)9 add_twice(3,data)

10 print(data)11

12 main()

f:

x

lst

main:6

1

list

As before, main is called at the start of this program. We createa new stack frame for it.

3/3

Page 21: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Lists Example

1 def add_twice(x,lst):2 lst.append(x)3 lst.append(x)4

5 def main():6 data = [1]7 add_twice(2,data)8 print(data)9 add_twice(3,data)

10 print(data)11

12 main()

f:

x

lst

main:6

1

list

Line 6 of main creates a new list containing just the value 1. Areference to that list is stored in the data variable. We representthe list by using a rounded box; we represent the reference as anarrow. 3/3

Page 22: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Lists Example

1 def add_twice(x,lst):2 lst.append(x)3 lst.append(x)4

5 def main():6 data = [1]7 add_twice(2,data)8 print(data)9 add_twice(3,data)

10 print(data)11

12 main()

f:

x

lst

main:7

data 1

list

Line 6 of main creates a new list containing just the value 1. Areference to that list is stored in the data variable. We representthe list by using a rounded box; we represent the reference as anarrow. 3/3

Page 23: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Lists Example

1 def add_twice(x,lst):2 lst.append(x)3 lst.append(x)4

5 def main():6 data = [1]7 add_twice(2,data)8 print(data)9 add_twice(3,data)

10 print(data)11

12 main()

f:

x

lst

main:7

data 1

list

Line 7 of main is a function call. Just as before, we create anew stack frame and copy each argument into its correspondingparameter. Here, we copy the value 2 into the variable x and wecopy the reference from data into the variable lst. 3/3

Page 24: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Lists Example

1 def add_twice(x,lst):2 lst.append(x)3 lst.append(x)4

5 def main():6 data = [1]7 add_twice(2,data)8 print(data)9 add_twice(3,data)

10 print(data)11

12 main()

f:2

x

lst

2

main:7

data 1

list

Line 7 of main is a function call. Just as before, we create anew stack frame and copy each argument into its correspondingparameter. Here, we copy the value 2 into the variable x and wecopy the reference from data into the variable lst. 3/3

Page 25: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Lists Example

1 def add_twice(x,lst):2 lst.append(x)3 lst.append(x)4

5 def main():6 data = [1]7 add_twice(2,data)8 print(data)9 add_twice(3,data)

10 print(data)11

12 main()

f:2

x

lst

2

main:7

data 1

list

Line 2 of add_twice appends a copy of the value in x to theend of the list. Here, that value is 2. We change the list objectin our diagram to reflect this.

3/3

Page 26: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Lists Example

1 def add_twice(x,lst):2 lst.append(x)3 lst.append(x)4

5 def main():6 data = [1]7 add_twice(2,data)8 print(data)9 add_twice(3,data)

10 print(data)11

12 main()

f:3

x

lst

2

main:7

data 1 2

list

Line 2 of add_twice appends a copy of the value in x to theend of the list. Here, that value is 2. We change the list objectin our diagram to reflect this.

3/3

Page 27: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Lists Example

1 def add_twice(x,lst):2 lst.append(x)3 lst.append(x)4

5 def main():6 data = [1]7 add_twice(2,data)8 print(data)9 add_twice(3,data)

10 print(data)11

12 main()

f:3

x

lst

2

main:7

data 1 2

list

Of course, line 3 does the same thing; this adds another 2 to ourlist. Note that this function doesn’t return anything; it just addsto the list.

3/3

Page 28: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Lists Example

1 def add_twice(x,lst):2 lst.append(x)3 lst.append(x)4

5 def main():6 data = [1]7 add_twice(2,data)8 print(data)9 add_twice(3,data)

10 print(data)11

12 main()

f:...

x

lst

2

main:7

data 1 2 2

list

Of course, line 3 does the same thing; this adds another 2 to ourlist. Note that this function doesn’t return anything; it just addsto the list.

3/3

Page 29: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Lists Example

1 def add_twice(x,lst):2 lst.append(x)3 lst.append(x)4

5 def main():6 data = [1]7 add_twice(2,data)8 print(data)9 add_twice(3,data)

10 print(data)11

12 main()

f:...

x

lst

2

main:7

data 1 2 2

list

Once we’re finished with the add_twice function, we destroy itsstack frame and return to executing main.

3/3

Page 30: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Lists Example

1 def add_twice(x,lst):2 lst.append(x)3 lst.append(x)4

5 def main():6 data = [1]7 add_twice(2,data)8 print(data)9 add_twice(3,data)

10 print(data)11

12 main()

f:

x

lst

2

main:8

data 1 2 2

list

Line 8 of main prints the contents of the list to which datarefers. Because of the call to add_twice, this list changed. Somain prints “[1,2,2]”.

3/3

Page 31: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Lists Example

1 def add_twice(x,lst):2 lst.append(x)3 lst.append(x)4

5 def main():6 data = [1]7 add_twice(2,data)8 print(data)9 add_twice(3,data)

10 print(data)11

12 main()

f:

x

lst

2

main:9

data 1 2 2

list

Line 9 of main calls add_twice again. Just as last time, we copythe arguments into their respective parameters. This time, x isset to 3; lst is still set to the same reference as data.

3/3

Page 32: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Lists Example

1 def add_twice(x,lst):2 lst.append(x)3 lst.append(x)4

5 def main():6 data = [1]7 add_twice(2,data)8 print(data)9 add_twice(3,data)

10 print(data)11

12 main()

f:2

x

lst

2

3

main:9

data 1 2 2

list

Line 9 of main calls add_twice again. Just as last time, we copythe arguments into their respective parameters. This time, x isset to 3; lst is still set to the same reference as data.

3/3

Page 33: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Lists Example

1 def add_twice(x,lst):2 lst.append(x)3 lst.append(x)4

5 def main():6 data = [1]7 add_twice(2,data)8 print(data)9 add_twice(3,data)

10 print(data)11

12 main()

f:2

x

lst

2

3

main:9

data 1 2 2

list

Once again, add_twice adds the value contained in x to the listreferenced by lst; it does this twice.

3/3

Page 34: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Lists Example

1 def add_twice(x,lst):2 lst.append(x)3 lst.append(x)4

5 def main():6 data = [1]7 add_twice(2,data)8 print(data)9 add_twice(3,data)

10 print(data)11

12 main()

f:3

x

lst

2

3

main:9

data 1 2 2 3

list

Once again, add_twice adds the value contained in x to the listreferenced by lst; it does this twice.

3/3

Page 35: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Lists Example

1 def add_twice(x,lst):2 lst.append(x)3 lst.append(x)4

5 def main():6 data = [1]7 add_twice(2,data)8 print(data)9 add_twice(3,data)

10 print(data)11

12 main()

f:...

x

lst

2

3

main:9

data 1 2 2 3 3

list

Once again, add_twice adds the value contained in x to the listreferenced by lst; it does this twice.

3/3

Page 36: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Lists Example

1 def add_twice(x,lst):2 lst.append(x)3 lst.append(x)4

5 def main():6 data = [1]7 add_twice(2,data)8 print(data)9 add_twice(3,data)

10 print(data)11

12 main()

f:...

x

lst

2

3

main:10

data 1 2 2 3 3

list

We finish add_twice, discarding its stack frame. We return tomain, where line 10 prints the contents of the list. Because ithas been changed again, we print [1,2,2,3,3] this time.

3/3

Page 37: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Lists Example

1 def add_twice(x,lst):2 lst.append(x)3 lst.append(x)4

5 def main():6 data = [1]7 add_twice(2,data)8 print(data)9 add_twice(3,data)

10 print(data)11

12 main()

f:...

x

lst

2

3

main:...

data 1 2 2 3 3

list

With that, the program is finished.

3/3

Page 38: Python (v3) Stack Frame Examples · stack frame and creating an object on the heap for the value. We draw an arrow from the variable to its value. 2/3. Basic Example 1 def f(x,y):

Lists Example

1 def add_twice(x,lst):2 lst.append(x)3 lst.append(x)4

5 def main():6 data = [1]7 add_twice(2,data)8 print(data)9 add_twice(3,data)

10 print(data)11

12 main()

f:...

x

lst

2

3

main:

data 1 2 2 3 3

list

With that, the program is finished.

3/3