2C Slides
2C Slides
Navigation tip for web slides: press ? to see keyboard navigation controls.
INTRODUCTION
LEARNING OBJECTIVES
In this lecture, you will learn to:
Define scope, local variable, and visualize local variables using the
value-based memory model.
Write and run tests using doctest and pytest
Import Python modules to use functions (and data types) defined
within them
LOCAL VARIABLES AND
FUNCTION SCOPE
SCOPE
The scope of a variable is where in the code it can be accessed.
Functions have local scope: their variables can only be accessed
inside the function body.
def square(x: float) -> float:
"""Return x squared.
>>> square(3.0)
9.0
>>> square(2.5)
6.25
"""
return x ** 2
>>> n = 10.0
>>> result = square(n + 3.
CORRECT VERSION
__main__
def square(x: float) -> fl (console)
"""Return x squared. Variable Value
n 10.0
>>> square(3.0)
9.0 square
>>> square(2.5) Variable Value
6.25
x 13.5
"""
return x ** 2
>>> n = 10.0
>>> result = square(n + 3.
SHOWING THE CURRENT FUNCTION
__main__
def square(x: float) -> fl Variable Value
"""Return x squared.
n 10.0
>>> n = 10.0
>>> result = square(n + 3.
NOTE: PYCHARM DEBUGGER
The PyCharm debugger is a powerful tool that lets us run Python
code one statement at a time, pausing at each statement so that we
can see exactly what line of code is being executed and what the
state of the variables are at that point.
With it, we can pause the Python interpreter, see the state of
variables at that point, and then manually cause the Python
interpreter to execute subsequent code one statement at a time.
Let’s do a demo!
REFERENCE NOTES ON USING THE
DEBUGGER
1. Click on the line number (directly to the left of the the code) of the
line you want to pause before. After clicking there, you will see a
red circle appear in the margin. This is called a breakpoint
2. In the Python console, click on the green “bug” icon on the left-
hand side. The hover text for this button is “Attach Debugger”,
which, well, “attaches” the PyCharm debugger to the running
Python console.
REFERENCE NOTES ON USING THE
DEBUGGER (CONT’D)
3. Then, call the function which has the breakpoint within the
console. As soon as the breakpoint is reached, the normal
program execution pauses, and PyCharm enters “debugging
mode”.
4. Note that the PyCharm debugger always highlights the next line
of code that will be executed. Click on the leftmost button in the
debugger toolbar, whose hover text is “Step Over” to execute the
next line (and watch the values get updated accordingly). You can
repeatedly use the “Step Over” button to execute each line.
AFTER A FUNCTION RETURNS
__main__
def square(x: float) -> fl Variable Value
"""Return x squared.
n 10.0
>>> n = 10.0
>>> result = square(n + 3.
EXERCISE 1 & 2: FUNCTION SCOPE AND
LOCAL VARIABLES AND THE VALUE-
BASED MEMORY MODEL
TESTING YOUR
FUNCTIONS
DOCTEST EXAMPLES
"""
>>> result = divides_by({2, 3, 20}, 2)
>>> result == {2: True, 3: False, 20: True}
True
"""
if __name__ == '__main__':
import doctest # import the doctest library
doctest.testmod() # run the tests
Demo!
TWO WARNINGS ABOUT doctest
1. If all tests pass, you won’t see any output by default. (But can call
doctest.testmod(verbose=True) instead.)
2. Tests must be formatted correctly, identical to Python console.
IMPORTING MODULES
So far: builtin data types, operations, and functions.
But there’s more that comes with Python!
TERMINOLOGY
A file containing Python code (.py) is called a module.
import <module_name>
This statement:
1. Finds a Python module with the corresponding name.
2. Runs that file, storing all defined functions/data types in a new
variable (the <module_name>).
DEMO: MODULE math
import math
math.sqrt
math.dist
math.sin
math.asin
TESTING WITH pytest
LIMITATIONS OF doctest
A function’s doctest examples are primarily about communicating
purpose, not verifying correctness.
TEST TERMINOLOGY
A unit test is a block of code that checks for the correct behaviour of
a function for one specific input.
A test suite is a collection of tests that check the behaviour of a
function of (usually small) set of functions.
CHOOSING GOOD TEST CASES
When you’re writing tests, we cannot feasibly test every single
possible value.
Instead, we choose some meaningful categories, and then we pick a
representative test value from each category.
CHOOSING GOOD TEST CASES
Properties we may consider for our meaningful categories:
Note: these categories are just general guidelines. They are not
always going to be relevant, and are usually not very clear-cut (there
can be overlap among them).
WRITING TEST CASES USING pytest
Demo!
EXERCISE 3: WRITING TEST CASES
SUMMARY
TODAY YOU LEARNED TO…
Define scope, local variable, and visualize local variables using the
value-based memory model.
Write and run tests using doctest and pytest
Import Python modules to use functions (and data types) defined
within them
UPCOMING TASKS
Tomorrow is our first checkpoint quiz day!
Attend the tutorial you are officially registered for on ACORN.
The quizzes are hand-written, closed-book.
There are two short quizzes tomorrow: first is on Chapter 1, second
is on Chapter 2.
Next week:
Read Chapter 3
Complete Week 3 Preparation on Quercus (both the Quercus quiz
and the MarkUs prep!)
Assignment 1 will be released by Monday :)