Students will be able to...
- Define and identify scope, aliasing, stack diagram, stack trace.
- Demonstrate that changing a list inside a function updates the list outside of the function.
- Demonstrate that updating variables inside a function does not affect the variable outside of the function.
- Demonstrate the use of global variables.
- Draw a simple stack diagram.
- 3.04 Slide Deck
- Do Now
- Lab - Aliasing & Scope (docx) (pdf)
- Read through the Do Now, lesson, and lab so that you are familiar with the requirements and can assist students.
- Associated Readings 3.4
Duration | Description |
---|---|
5 Minutes | Do Now |
20 Minutes | Lesson |
20 Minutes | Lab/Review |
10 Minutes | Debrief |
- Students have a chance to think about what & discuss what concepts they have been most challenged by.
- Next, students practice passing a list as an argument and updating that list within the function.
- Discuss what students observed in the Do Now and take time, if needed, to go over questions about concepts that students find challenging.
-
Explain the concept of aliasing.
-
You can draw on the board a diagram of the variable pointing to a list.
-
Note that when passing the location of a list you are not passing the actual value, so the list can be changed.
-
Video Explanation of Aliasing:
-
Explain to students that variable scope is the part of a program where a variable is accessible.
-
A variable which is defined in the main body of a file is called a global variable.
-
Video explanation of Variable Scope
- Explain global variables are often used for constants.
- Any variable created inside of a function is a local variable.
- Variables in functions include the function parameters, the variables defined in the function, and variables declared as global.
- Local variables of functions can't be accessed from outside when the function call has finished.
- Discuss that programming languages frequently have conventions. It helps make code more readable, but isn't essential to functionality (in most cases).
- Here are some examples:
- Function and variable names should be lowercase, with words separated by underscores as necessary to improve readability.
- Constants are usually defined on a module level and written in all capital letters with underscores separating words. Examples include MAX_OVERFLOW and TOTAL.
- Demonstrate how to draw the Stack Diagrams shown in the course book (found in section 3.4) and explain how they show the scope of variables as they related to functions.
- Point out the error messages that will occur if you use a variable out of its scope.
- Show an example of a stack trace for a simple error in the Do Now Part 2 sample code.
- The error message shows evidence of how Python keeps track a "stack" of functions, each one calling the next one, so that it can make available only the variables that are in scope in the current function.
- Help students follow their program to understand how the code is working.
- Explain how the use of print statements throughout your code can let you know where in the program things are not operating as expected.
- This lab has students running code that gets them thinking about aliasing and scope. They must also create a stack diagram for a program to show their understanding of scope.
- Take time to review the concepts covered today: scope, aliasing, and stack diagrams.
- Call a few students to the board to draw their stack diagrams from the lab and talk through them.
If students are moving quickly, they can look ahead at the project spec or research the game Oregon Trail for context.