Skip to content
Snippets Groups Projects
Commit 9601f2e5 authored by LOUIS TYRRELL OLIPHANT's avatar LOUIS TYRRELL OLIPHANT
Browse files

added in lec 07 scope

parent 11fde761
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:8aca3edd-417f-45d9-86a8-96cecdca7c21 tags:
## Warmup
%% Cell type:code id:d95fb89e-e5e9-4051-b2d9-3d3b47d05a47 tags:
``` python
# Warmup 1
# TODO: Create a function called add_em_up() which takes in three inputs
# and returns the sum of those three numbers
## Function definition goes here
# Your function should work correctly with the following function call
print(add_em_up(1,2,3))
```
%% Cell type:code id:3b3016f9-b1ba-4ee6-b9f7-cf3dc0c3e3d3 tags:
``` python
# Warmup 2
# TODO: Create a function called box() which takes 3 inputs:
# size (which should have a default value of 3)
# corner_symbol (which should have a default value of '+')
# side_symbol (which should default to the value of '*')
#
# The function should return a box made of characters of the proper size.
# The function should work with the function calls at the bottom of this cell
# Function definition goes here
# Function should work correctly with all of the following function calls
print(box())
# output should be
# +***+
# * *
# * *
# * *
# +***+
print(box(1,corner_symbol='*',side_symbol='-'))
# output should be
# *-*
# - -
# *-*
# Write another function call using the default value for the corner symbol, but
# provide new values for the size and side_symbol
```
%% Cell type:markdown id:14a4daee-908b-4a68-90c9-3df5b3edb502 tags:
## Reminder
- Recall how to define functions using a header and body
- Assign parameters their values using positional arguments, keyword arguments, and default values
- Return a value from a function
- Use the debugger to step through execution of a function
%% Cell type:markdown id:82184a18 tags:
# Function Scope
## Reading
* [Python for Everybody, 5.11 - 5.16](https://runestone.academy/ns/books/published//py4e-int/functions/toctree.html)
* [Downey Chapter 3 - Parameters and Arguments to the end of the chapter](https://greenteapress.com/wp/think-python-2e/)
## Learning Objectives
* Explain the concept of **scope** and the difference between a **local** variable and a **global** variable.
* Label variables as local or global and state the scope of the variable
* Explain the terms **call stack** and **frame**
* Identify as code is executed when frames are created and destroyed on the call stack and what the content of the frames will be
#### Exam Conflict Form
* [Exam 1 - Feb 19, 7:30 pm](https://cs220.cs.wisc.edu/s25/surveys.html)
* [Exam 2 - Apr 02, 7:30 pm](https://cs220.cs.wisc.edu/s25/surveys.html)
* [Final - May 08, 7:25 pm](https://cs220.cs.wisc.edu/s25/surveys.html)
%% Cell type:markdown id:d1cd1813-b3bf-4069-a431-02b8c6d5237d tags:
## Definitions
The **scope** of a variable is the area of the program where the variable is recognized and can be used. Python has two scopes, **global** (the variable can be used everywhere) and **local** (the variable can be used within the function in which it was defined).
As a program is executed, the **callstack** stores information (i.e. variables) about the active local environments. Each time a function is called, a new **frame** is placed on the callstack. Each time a function finishes, its frame is removed from the callstack.
Turn on the debugger and place a breakpoint on the first line of code in the cell below and "step in" the execution of the code in the cell until the cell finishes. Then do the same for the cell below that. Watch the "callstack" and "variables" portion of the debugger.
%% Cell type:code id:6a805200-5bf8-42d4-a15a-6257d1a93416 tags:
``` python
def f():
x = 1
print(x)
def g():
y = 2
print(y)
def h():
z = 3
print(z)
f()
g()
h()
```
%% Cell type:code id:91b01530-6e3d-4098-bf83-f2a44100644c tags:
``` python
def f():
x = 1
print(x)
g()
def g():
y = 2
print(y)
h()
def h():
z = 3
print(z)
f()
```
%% Cell type:markdown id:300337e1 tags:
Now do the same to practice pieces of code at the [Interactive Python Tutor](https://cs220.cs.wisc.edu/s23/materials/lec-07.html).
Notice the following:
- A global scope holds the variables defined outside of any function AND the functions themselves
- When a function starts, a new frame is placed on the callstack
- When a function ends, the frame is removed from the callstack
- The local variables (i.e. those created inside of a function) are placed within that function's frame
- A function can call another function, causing multiple frames to be on the callstack at the same time
Be aware that the "variable" section in the debugger has a drop-down letting you switch between the global and the local frame. Also notice that you can click on the items in the callstack to have the "local" variables be displayed for that active frame.
Use the debugger with the code in the cell below to step through the execution of the code. Watch as each of the local frames are created and use the callstack switch between these local frames to see the variable x in each of those frames.
%% Cell type:code id:253366ee-3eab-49f4-a137-e78885124ad9 tags:
``` python
def f():
x = 1
print(x)
g()
def g():
x = 2
print(x)
h()
def h():
x = 3
print(x)
f()
```
%% Cell type:markdown id:96c7c201-3635-46c8-a227-cedbbb6722b7 tags:
## Using Python Tutor
Now use the [Python Tutor](https://cs220.cs.wisc.edu/s23/materials/lec-07.html) to watch and execute the code for each of the lessons. You can also execute the code using the debugger. Make sure you understand each of the lessons or concepts being taught.
**Lesson 1: Functions don't execute unless they are called**
%% Cell type:code id:c6932120-b5dd-48e2-836c-a889a7b27a9d tags:
``` python
def set_x():
x = 100
print(x)
```
%% Cell type:markdown id:54aabfd7-3572-40bc-bb25-286947f9b14a tags:
**Lesson 2: Variables created in a function die after function returns**
%% Cell type:code id:8b24429f-972d-4957-9c8a-39b182157ae7 tags:
``` python
def set_x():
x = 100
set_x()
print(x)
```
%% Cell type:markdown id:7b3bccbb-1eba-4490-a4b4-441ce937a3ab tags:
**Lesson 3: Variables start fresh every time a function is called again**
%% Cell type:code id:244882dd-818f-419c-98d3-4cafa60310ff tags:
``` python
def count():
x = 1
x += 1
print(x)
count()
count()
count()
```
%% Cell type:markdown id:1ceeba4d-e2c6-45d6-80b6-ad90733723a4 tags:
**Lesson 4: you can't see the variables of other function invocations, even those that call you**
%% Cell type:code id:ca7b26b0-6518-4061-972d-d5caec39a298 tags:
``` python
def display_x():
print(x)
def main():
x = 100
display_x()
main()
```
%% Cell type:markdown id:62f3a27f-3db3-4492-ae0d-d1ca295e66e9 tags:
**Lesson 5: you can generally just use global variables inside a function**
%% Cell type:code id:67257372-92a2-46e7-81c2-8746004cacc5 tags:
``` python
msg = 'hello' # global, outside any func
def greeting():
print(msg)
print('before: ' + msg)
greeting()
print('after: ' + msg)
```
%% Cell type:markdown id:0964c63c-6e0c-4234-b347-eda94c5f3b73 tags:
**Lesson 6: if you do an assignment to a variable in a function, Python assumes you want it local**
%% Cell type:code id:124ba158-7ee0-49ac-85ad-8dcb767d9c9c tags:
``` python
msg = 'hello'
def greeting():
msg = 'welcome!'
print('greeting: ' + msg)
print('before: ' + msg)
greeting()
print('after: ' + msg)
```
%% Cell type:markdown id:a7df0197-9e2a-42a6-a7b9-fd062436783d tags:
**Lesson 7: assignment to a variable should be before its use in a function, even if there's a a global variable with the same name**
%% Cell type:code id:f516f74e-dfe0-4726-9203-5082b81c123a tags:
``` python
msg = 'hello'
def greeting():
print('greeting: ' + msg)
msg = 'welcome!'
print('before: ' + msg)
greeting()
print('after: ' + msg)
```
%% Cell type:markdown id:a1f6e7e9-e48e-4de9-8b01-6308a1a91f04 tags:
**Lesson 8: use a global declaration to prevent Python from creating a local variable when you want a global variable**
%% Cell type:code id:04962da5-4178-4361-929b-f068703c2998 tags:
``` python
msg = 'hello'
def greeting():
global msg
print('greeting: ' + msg)
msg = 'welcome!'
print('before: ' + msg)
greeting()
print('after: ' + msg)
```
%% Cell type:markdown id:205e3043-afd2-4c5f-b37a-3bc8067f2f31 tags:
**Lesson 9: in Python, arguments are "passed by value", meaning reassignments to a parameter don't change the argument outside**
%% Cell type:code id:4c2188eb-eb00-4023-92a0-8be153cef28e tags:
``` python
def f(x):
x = 'B'
print('inside: ' + x)
val = 'A'
print('before: ' + val)
f(val)
print('after: ' + val)
```
%% Cell type:markdown id:4633aafe-0cdc-4ded-a867-ad232e16c7b4 tags:
**Lesson 10: it's irrelevant whether the argument (outside) and parameter (inside) have the same variable name**
%% Cell type:code id:733ba7d9-6aa9-42c2-bbbd-97d55a86c191 tags:
``` python
x = 'A'
def f(x):
x = 'B'
print('inside: ' + x)
print('before: ' + x)
f(x)
print('after: ' + x)
```
%% Cell type:markdown id:066458da tags:
## Summary
You have learned about variable **scope**, specifically **local** and **global** scope. You have watched as **frames** are created and placed on the **callstack**. These frames are used for storing local variables. And you have learned about the rules for what variables you can access from within a function.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment