generated from PovertyAction/ipa-python-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path17-scope.qmd
More file actions
150 lines (111 loc) · 4.06 KB
/
17-scope.qmd
File metadata and controls
150 lines (111 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
---
title: "Variable Scope"
abstract: |
Identify local and global variables. Identify parameters as local variables. Read a traceback and determine the file, function, and line number on which the error occurred, the type of error, and the error message.
date: last-modified
format:
html: default
# Authors
authors-ipa:
- "[Author Name](https://poverty-action.org/people/author_name)"
# Contributors
contributors:
- "[Contributor Name](https://poverty-action.org/people/contributor_name)"
keywords: ["Python", "Programming", "Tutorial", "Data Science", "Jupyter"]
license: "CC BY 4.0"
---
::: {.callout-note}
## Learning Objectives
- Identify local and global variables.
- Identify parameters as local variables.
- Read a traceback and determine the file, function, and line number on which the error occurred, the type of error, and the error message.
::: {.callout-tip}
## Key Questions
- How do function calls actually work?
- How can I determine where errors occurred?
## The scope of a variable is the part of a program that can 'see' that variable
- There are only so many sensible names for variables.
- People using functions shouldn't have to worry about
what variable names the author of the function used.
- People writing functions shouldn't have to worry about
what variable names the function's caller uses.
- The part of a program in which a variable is visible is called its *scope*.
```python
pressure = 103.9
def adjust(t):
temperature = t * 1.43 / pressure
return temperature
```
- `pressure` is a *global variable*.
- Defined outside any particular function.
- Visible everywhere.
- `t` and `temperature` are *local variables* in `adjust`.
- Defined in the function.
- Not visible in the main program.
- Remember: a function parameter is a variable
that is automatically assigned a value when the function is called.
```python
print('adjusted:', adjust(0.9))
print('temperature after call:', temperature)
```
```output
adjusted: 0.01238691049085659
```
```error
Traceback (most recent call last):
File "/Users/swcarpentry/foo.py", line 8, in <module>
print('temperature after call:', temperature)
NameError: name 'temperature' is not defined
```
::: {.callout-note}
## Exercise: Local and Global Variable Use
Trace the values of all variables in this program as it is executed.
(Use '---' as the value of variables before and after they exist.)
```python
limit = 100
def clip(value):
return min(max(0.0, value), limit)
value = -22.5
print(clip(value))
```
::: {.callout-note}
## Exercise: Reading Error Messages
Read the traceback below, and identify the following:
1. How many levels does the traceback have?
2. What is the file name where the error occurred?
3. What is the function name where the error occurred?
4. On which line number in this function did the error occur?
5. What is the type of error?
6. What is the error message?
```error
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-2-e4c4cbafeeb5> in <module>()
1 import errors_02
----> 2 errors_02.print_friday_message()
/Users/ghopper/thesis/code/errors_02.py in print_friday_message()
13
14 def print_friday_message():
---> 15 print_message("Friday")
/Users/ghopper/thesis/code/errors_02.py in print_message(day)
9 "sunday": "Aw, the weekend is almost over."
10 }
---> 11 print(messages[day])
12
13
KeyError: 'Friday'
```
::: {.callout-tip collapse="true"}
## Solution
## Solution
1. Three levels.
2. `errors_02.py`
3. `print_message`
4. Line 11
5. `KeyError`. These errors occur when we are trying to look up a key that does not exist (usually in a data
structure such as a dictionary). We can find more information about the `KeyError` and other built-in exceptions
in the [Python docs](https://docs.python.org/3/library/exceptions.html#KeyError).
6. `KeyError: 'Friday'`
::: {.callout-important}
## Key Points
- The scope of a variable is the part of a program that can 'see' that variable.