|
|
<!doctype html>
|
|
|
<html lang="en">
|
|
|
<head>
|
|
|
<meta charset="utf-8">
|
|
|
|
|
|
<title></title>
|
|
|
|
|
|
<meta name="description" content="">
|
|
|
|
|
|
|
|
|
<meta name="apple-mobile-web-app-capable" content="yes" />
|
|
|
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
|
|
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
|
|
|
|
|
|
<!-- For syntax highlighting -->
|
|
|
<link rel="stylesheet" href="../../../../lib/css/zenburn.css">
|
|
|
<link rel="stylesheet" href="../../../../lib/css/prism.css">
|
|
|
|
|
|
<link rel="stylesheet" href="../../../../css/reveal.css">
|
|
|
<link rel="stylesheet" href="../../../../css/theme/ga-title.css" id="theme">
|
|
|
|
|
|
<!--[if lt IE 9]>
|
|
|
<script src="lib/js/html5shiv.js"></script>
|
|
|
<![endif]-->
|
|
|
|
|
|
<link rel="stylesheet" type="text/css" href="https://s3.amazonaws.com/python-ga/proxima-nova/fonts.css" />
|
|
|
|
|
|
</head>
|
|
|
|
|
|
<body class="language-javascript">
|
|
|
|
|
|
<div class="reveal">
|
|
|
|
|
|
<!-- Any section element inside of this container is displayed as a slide -->
|
|
|
<div class="slides">
|
|
|
|
|
|
|
|
|
<!--
|
|
|
title: Variable Scope
|
|
|
type: lesson
|
|
|
duration: "00:30"
|
|
|
creator: Brandi Butler
|
|
|
-->
|
|
|
<section id="section" class="level2 separator">
|
|
|
<h2><img src="https://s3.amazonaws.com/python-ga/images/GA_Cog_Medium_White_RGB.png" /></h2>
|
|
|
<h1>
|
|
|
Unit 3 Lab: Variable Scope
|
|
|
</h1>
|
|
|
<!--
|
|
|
|
|
|
## Overview
|
|
|
This lesson introduces local and global scope with a few examples. If there is time, give students examples of broken programs that mix up global and local scopes, and ask them to fix it.
|
|
|
|
|
|
## Learning Objectives
|
|
|
In this lesson, students will:
|
|
|
|
|
|
* Define variable scope.
|
|
|
* Use the global keyword to access global variables.
|
|
|
* Explain the order of scope precedence that Python follows when resolving variable names.
|
|
|
|
|
|
|
|
|
## Duration
|
|
|
20 minutes
|
|
|
|
|
|
## Suggested Agenda
|
|
|
|
|
|
| Time | Activity |
|
|
|
| --- | --- |
|
|
|
| 0:00 - 0:03 | Welcome |
|
|
|
| 0:03 - 0:08 | Local Scope |
|
|
|
| 0:08 - 0:18 | Global scope |
|
|
|
| 0:18 - 0:20 | Summary |
|
|
|
|
|
|
## Differentiation and Extensions
|
|
|
- There are no exercises involving classes, built-in scope, or enclosed scope. If there is time and your students seem confident, create some — or challenge your students to come up with examples themselves.
|
|
|
|
|
|
## In Class: Materials
|
|
|
- Projector
|
|
|
- Internet connection
|
|
|
- Python 3
|
|
|
-->
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="lesson-objectives" class="level2">
|
|
|
<h2>Lesson Objectives</h2>
|
|
|
<p><em>After this lesson, you will be able to…</em></p>
|
|
|
<ul>
|
|
|
<li>Define variable scope.</li>
|
|
|
<li>Use the global keyword to access global variables.</li>
|
|
|
<li>Explain the order of scope precedence that Python follows when resolving variable names.</li>
|
|
|
</ul>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>Jot these on the board for reference.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="discussion-delivering-a-letter" class="level2">
|
|
|
<h2>Discussion: Delivering a Letter</h2>
|
|
|
<p>What if someone wanted to send Brandi a letter?</p>
|
|
|
<p>If you just had “For Brandi,” the mail carrier would give the letter to the first Brandi they see!</p>
|
|
|
<p>They’d look:</p>
|
|
|
<ul>
|
|
|
<li>First in the class. Is there a “Brandi” here? They get the letter!</li>
|
|
|
<li>No? OK, look in the town. Is there a “Brandi” here? They get the letter!</li>
|
|
|
<li>No? OK, look in the state. Is there a “Brandi” here? They get the letter!</li>
|
|
|
</ul>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tip:</strong></p>
|
|
|
<ul>
|
|
|
<li>Don’t mention programming here. Just make sure the class is clear on the idea of scope and how, if we aren’t specific, we’ll look first in town, then state — continue getting wider.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="discussion-your-address" class="level2">
|
|
|
<h2>Discussion: Your Address</h2>
|
|
|
<p>That’s why <strong>scope</strong> matters. We might have to get more specific. To correctly deliver the letter, if the mail carrier only looked in the scope of:</p>
|
|
|
<p>Your class:</p>
|
|
|
<ul>
|
|
|
<li>You’re probably the only Brandi.</li>
|
|
|
<li>“For Brandi” is fine.</li>
|
|
|
</ul>
|
|
|
<p>Your town:</p>
|
|
|
<ul>
|
|
|
<li>There might be multiple Brandis in the town.</li>
|
|
|
<li>“For Brandi, on Main Street” is a bit more specific.</li>
|
|
|
</ul>
|
|
|
<p>In your state:</p>
|
|
|
<ul>
|
|
|
<li>There are multiple Main Streets in New York!</li>
|
|
|
<li>“For Brandi, on Main Street in Brooklyn” is more specific.</li>
|
|
|
</ul>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="discussion-what-is-x" class="level2">
|
|
|
<h2>Discussion: What Is <code>x</code>?</h2>
|
|
|
<p>Python has <strong>scope</strong>, too. We can have many variables with the same name, and Python will look for the most specific one.</p>
|
|
|
<p>In different scopes, you can reuse the same name. Each one is a <em>completely different</em> variable.</p>
|
|
|
<p>Functions and classes create individual <strong>local scopes</strong>. A <strong>local variable</strong> doesn’t exist outside its local function or class scope.</p>
|
|
|
<div class="sourceCode" id="cb1"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb1-1" data-line-number="1"><span class="kw">def</span> my_func1():</a>
|
|
|
<a class="sourceLine" id="cb1-2" data-line-number="2"> x <span class="op">=</span> <span class="dv">1</span> <span class="co"># This is a LOCAL variable.</span></a>
|
|
|
<a class="sourceLine" id="cb1-3" data-line-number="3"> <span class="bu">print</span>(x) <span class="co"># 1</span></a>
|
|
|
<a class="sourceLine" id="cb1-4" data-line-number="4"></a>
|
|
|
<a class="sourceLine" id="cb1-5" data-line-number="5"><span class="kw">def</span> my_func2():</a>
|
|
|
<a class="sourceLine" id="cb1-6" data-line-number="6"> x <span class="op">=</span> <span class="dv">5</span> <span class="co"># This is a DIFFERENT local variable.</span></a>
|
|
|
<a class="sourceLine" id="cb1-7" data-line-number="7"> <span class="bu">print</span>(x) <span class="co">#5</span></a>
|
|
|
<a class="sourceLine" id="cb1-8" data-line-number="8"></a>
|
|
|
<a class="sourceLine" id="cb1-9" data-line-number="9"><span class="bu">print</span>(x) <span class="co"># x is OUT OF SCOPE - no x exists here!</span></a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>Walk through this carefully!</li>
|
|
|
<li>Run it in an interpreter, repl.it, or file to show it working (remove the last <code>print</code> to stop the error).</li>
|
|
|
<li>Terminology is next — just get students to understand the idea.</li>
|
|
|
</ul>
|
|
|
<p><strong>Talking Points:</strong></p>
|
|
|
<ul>
|
|
|
<li>Any variable declared or assigned inside of a function is local to that function.</li>
|
|
|
<li>This is the most specific level of scope and is, ideally, where most of your variables should be declared.</li>
|
|
|
<li>Only the function in which the variable was declared has access to this scope — i.e., the variable is out of scope for everything but that function.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="global-scope" class="level2">
|
|
|
<h2>Global Scope</h2>
|
|
|
<p>Variables that are in <strong>global scope</strong> can be accessed anywhere. - Python will check for a local variable before using a global one.</p>
|
|
|
<div class="sourceCode" id="cb2"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb2-1" data-line-number="1">x <span class="op">=</span> <span class="dv">2</span></a>
|
|
|
<a class="sourceLine" id="cb2-2" data-line-number="2"></a>
|
|
|
<a class="sourceLine" id="cb2-3" data-line-number="3"><span class="kw">def</span> my_func1():</a>
|
|
|
<a class="sourceLine" id="cb2-4" data-line-number="4"> x <span class="op">=</span> <span class="dv">1</span></a>
|
|
|
<a class="sourceLine" id="cb2-5" data-line-number="5"> <span class="bu">print</span>(x) <span class="co"># 1 - Python checks local scopes first.</span></a>
|
|
|
<a class="sourceLine" id="cb2-6" data-line-number="6"></a>
|
|
|
<a class="sourceLine" id="cb2-7" data-line-number="7"><span class="kw">def</span> my_func2():</a>
|
|
|
<a class="sourceLine" id="cb2-8" data-line-number="8"> x <span class="op">=</span> <span class="dv">5</span></a>
|
|
|
<a class="sourceLine" id="cb2-9" data-line-number="9"> <span class="bu">print</span>(x) <span class="co"># 5 - Python checks local scopes first.</span></a>
|
|
|
<a class="sourceLine" id="cb2-10" data-line-number="10"></a>
|
|
|
<a class="sourceLine" id="cb2-11" data-line-number="11">my_func1()</a>
|
|
|
<a class="sourceLine" id="cb2-12" data-line-number="12">my_func2()</a>
|
|
|
<a class="sourceLine" id="cb2-13" data-line-number="13"></a>
|
|
|
<a class="sourceLine" id="cb2-14" data-line-number="14"><span class="bu">print</span>(x) <span class="co"># 2 - Python found no local scope; prints global variable.</span></a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Talking Points:</strong></p>
|
|
|
<ul>
|
|
|
<li>If some variables are specifically local, what are the variables outside of a function or class called?</li>
|
|
|
<li>Any variable declared or assigned outside of any function or class is considered “global.”</li>
|
|
|
<li>Global variables are accessible from anywhere in the script. This is not necessarily a good thing, however, because those variables can be accessed, changed, or reassigned by anything, and this can lead to troublesome bugs.</li>
|
|
|
<li>This is another case where Python has our backs. It’s preventing us from making an accidental error that could easily occur in many other languages.</li>
|
|
|
<li>Python assumes local unless otherwise specified.
|
|
|
<ul>
|
|
|
<li>Meaning, these <code>x</code>s are three different variables.</li>
|
|
|
</ul></li>
|
|
|
<li>Python does this to prevent unexpected behavior and accidental bad practice.
|
|
|
<ul>
|
|
|
<li>It’s considered sloppy to have too many global variables.</li>
|
|
|
<li>If you have a large code base, you may have forgotten that you used a variable name elsewhere.</li>
|
|
|
<li>If you’re working on a team, another person may have used a variable name without your knowledge.</li>
|
|
|
</ul></li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="multiple-variables-one-name" class="level2">
|
|
|
<h2>Multiple Variables, One Name</h2>
|
|
|
<p>Use case: <code>x</code> and <code>y</code> are frequently used to represent numbers.</p>
|
|
|
<p>Scope is important so they don’t interact!</p>
|
|
|
<div class="sourceCode" id="cb3"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb3-1" data-line-number="1"><span class="kw">def</span> add(x, y):</a>
|
|
|
<a class="sourceLine" id="cb3-2" data-line-number="2"> <span class="cf">return</span> x <span class="op">+</span> y</a>
|
|
|
<a class="sourceLine" id="cb3-3" data-line-number="3"></a>
|
|
|
<a class="sourceLine" id="cb3-4" data-line-number="4"><span class="kw">def</span> subtract(x, y):</a>
|
|
|
<a class="sourceLine" id="cb3-5" data-line-number="5"> <span class="cf">return</span> x <span class="op">-</span> y</a>
|
|
|
<a class="sourceLine" id="cb3-6" data-line-number="6"></a>
|
|
|
<a class="sourceLine" id="cb3-7" data-line-number="7"><span class="kw">def</span> multiply(x, y):</a>
|
|
|
<a class="sourceLine" id="cb3-8" data-line-number="8"> <span class="cf">return</span> x <span class="op">*</span> y</a>
|
|
|
<a class="sourceLine" id="cb3-9" data-line-number="9"></a>
|
|
|
<a class="sourceLine" id="cb3-10" data-line-number="10"><span class="kw">def</span> divide(x, y):</a>
|
|
|
<a class="sourceLine" id="cb3-11" data-line-number="11"> <span class="cf">return</span> x <span class="op">/</span> y</a>
|
|
|
<a class="sourceLine" id="cb3-12" data-line-number="12"></a>
|
|
|
<a class="sourceLine" id="cb3-13" data-line-number="13">divide (<span class="dv">8</span>,<span class="dv">2</span>) <span class="co"># Returns 4</span></a>
|
|
|
<a class="sourceLine" id="cb3-14" data-line-number="14">multiply(<span class="dv">3</span>,<span class="dv">1</span>) <span class="co"># Returns 3</span></a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Talking Point:</strong></p>
|
|
|
<ul>
|
|
|
<li>Why would you want to have different variables with the same name? Do you expect each <code>x</code> and <code>y</code> in this code to perform independently?</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="we-do-accessing-scopes" class="level2">
|
|
|
<h2>We Do: Accessing Scopes</h2>
|
|
|
<p>Let’s start with global scope:</p>
|
|
|
<div class="sourceCode" id="cb4"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb4-1" data-line-number="1">foo <span class="op">=</span> <span class="dv">5</span></a>
|
|
|
<a class="sourceLine" id="cb4-2" data-line-number="2"><span class="bu">print</span>(foo)</a>
|
|
|
<a class="sourceLine" id="cb4-3" data-line-number="3">foo <span class="op">=</span> <span class="dv">7</span></a>
|
|
|
<a class="sourceLine" id="cb4-4" data-line-number="4"><span class="bu">print</span>(foo)</a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Talking Point:</strong></p>
|
|
|
<ul>
|
|
|
<li>Python makes it a little trickier than other languages to fiddle around with global variables if we’re not already in that scope. First, start up a blank script. The following line will assign a global variable named foo the value of <code>5</code>. We can easily reassign and access that variable with the following lines. That’s the global scope: There’s no restriction on accessing or mutating a variable.</li>
|
|
|
</ul>
|
|
|
<p><strong>Teaching Tip:</strong></p>
|
|
|
<ul>
|
|
|
<li>Run all the code in these slides in an interpreter for students to see. Encourage them to do this with you.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="we-do-accessing-local-scope" class="level2">
|
|
|
<h2>We Do: Accessing Local Scope</h2>
|
|
|
<p>What if we add a variable in a local function scope and try to access it from the global scope?</p>
|
|
|
<div class="sourceCode" id="cb5"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb5-1" data-line-number="1">foo <span class="op">=</span> <span class="dv">5</span></a>
|
|
|
<a class="sourceLine" id="cb5-2" data-line-number="2"></a>
|
|
|
<a class="sourceLine" id="cb5-3" data-line-number="3"><span class="co"># Delete your other code.</span></a>
|
|
|
<a class="sourceLine" id="cb5-4" data-line-number="4"><span class="co"># Add this function and print calls instead.</span></a>
|
|
|
<a class="sourceLine" id="cb5-5" data-line-number="5"><span class="kw">def</span> coolFunc():</a>
|
|
|
<a class="sourceLine" id="cb5-6" data-line-number="6"> bar <span class="op">=</span> <span class="dv">8</span></a>
|
|
|
<a class="sourceLine" id="cb5-7" data-line-number="7"></a>
|
|
|
<a class="sourceLine" id="cb5-8" data-line-number="8">coolFunc()</a>
|
|
|
<a class="sourceLine" id="cb5-9" data-line-number="9"><span class="bu">print</span>(foo)</a>
|
|
|
<a class="sourceLine" id="cb5-10" data-line-number="10"><span class="bu">print</span>(bar)</a></code></pre></div>
|
|
|
<p>It fails!</p>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Talking Points:</strong></p>
|
|
|
<ul>
|
|
|
<li>If you run this code, you will get an error: <code>NameError: name 'bar' is not defined.</code>.</li>
|
|
|
<li>The variable bar is only accessible from inside the <code>coolFunc()</code> function.</li>
|
|
|
<li>We called the <code>coolFunc()</code> function, but as soon as it finished running, the variable bar ceased to exist. Even while the function was running, it was only accessible to itself. But, <code>foo</code> in the global scope was still accessible.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="scope-can-be-tricky" class="level2">
|
|
|
<h2>Scope Can Be Tricky</h2>
|
|
|
<p>What do you think happened here?</p>
|
|
|
<div class="sourceCode" id="cb6"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb6-1" data-line-number="1">foo <span class="op">=</span> <span class="dv">5</span></a>
|
|
|
<a class="sourceLine" id="cb6-2" data-line-number="2"><span class="kw">def</span> incrementFoo():</a>
|
|
|
<a class="sourceLine" id="cb6-3" data-line-number="3"> foo <span class="op">=</span> <span class="dv">6</span></a>
|
|
|
<a class="sourceLine" id="cb6-4" data-line-number="4"> <span class="bu">print</span>(foo) <span class="co"># prints 6</span></a>
|
|
|
<a class="sourceLine" id="cb6-5" data-line-number="5"></a>
|
|
|
<a class="sourceLine" id="cb6-6" data-line-number="6"><span class="bu">print</span>(foo) <span class="co"># prints 5</span></a>
|
|
|
<a class="sourceLine" id="cb6-7" data-line-number="7">incrementFoo()</a>
|
|
|
<a class="sourceLine" id="cb6-8" data-line-number="8"><span class="bu">print</span>(foo) <span class="co"># prints 5</span></a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tip:</strong></p>
|
|
|
<ul>
|
|
|
<li>Spend some time here. Ensure student understanding.</li>
|
|
|
</ul>
|
|
|
<p><strong>Talking Points:</strong></p>
|
|
|
<ul>
|
|
|
<li>Hey! The variable <code>foo</code> went back to its old value after the function finished! Actually, not quite. Here’s what happened:
|
|
|
<ul>
|
|
|
<li>The line in the function where <code>foo</code> is assigned the value of <code>6</code> causes the creation of a new local variable.</li>
|
|
|
<li>We then set this variable’s value to <code>6</code>, the function prints the value, and the function finishes. However, the global variable <code>foo</code> was never touched by the function.</li>
|
|
|
</ul></li>
|
|
|
</ul>
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>Run this!</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="i-do-the-global-keyword" class="level2">
|
|
|
<h2>I Do: The Global Keyword</h2>
|
|
|
<p>You can call a global variable intentionally with <code>global</code>. * What do you think happens if you forget the <code>global</code> keyword?</p>
|
|
|
<div class="sourceCode" id="cb7"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb7-1" data-line-number="1">foo <span class="op">=</span> <span class="dv">5</span></a>
|
|
|
<a class="sourceLine" id="cb7-2" data-line-number="2"><span class="kw">def</span> incrementFoo():</a>
|
|
|
<a class="sourceLine" id="cb7-3" data-line-number="3"> <span class="kw">global</span> foo</a>
|
|
|
<a class="sourceLine" id="cb7-4" data-line-number="4"> foo <span class="op">+=</span> <span class="dv">1</span></a>
|
|
|
<a class="sourceLine" id="cb7-5" data-line-number="5"></a>
|
|
|
<a class="sourceLine" id="cb7-6" data-line-number="6"><span class="bu">print</span>(foo) <span class="co"># prints 5</span></a>
|
|
|
<a class="sourceLine" id="cb7-7" data-line-number="7">incrementFoo()</a>
|
|
|
<a class="sourceLine" id="cb7-8" data-line-number="8"><span class="bu">print</span>(foo) <span class="co"># prints 6!</span></a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Talking Points:</strong></p>
|
|
|
<ul>
|
|
|
<li>It is very clearly marked as <code>global</code>, so anyone using it is fully aware it’s global.</li>
|
|
|
<li>If you forget the <code>global</code> keyword, it simply becomes an unrelated local variable.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="we-do-global-vs.local" class="level2">
|
|
|
<h2>We Do: Global vs. Local</h2>
|
|
|
<p>In the following code, there are three <code>print</code> statements. Before you run the code, guess what those <code>print</code> statements will print.</p>
|
|
|
<iframe height="400px" width="100%" src="https://repl.it/@SuperTernary/python-programming-scope-quiz?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals">
|
|
|
</iframe>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Repl.it Note:</strong></p>
|
|
|
<pre><code># Global variable x:
|
|
|
x = 1
|
|
|
|
|
|
# Only local x in this function:
|
|
|
def my_func1():
|
|
|
x = 2 # This is a different, local x
|
|
|
print(x) # Print the local x
|
|
|
|
|
|
# Using global x:
|
|
|
def my_func2():
|
|
|
global x # We declare we'd like to use the global x.
|
|
|
print(x) # Print that global x.
|
|
|
x = 3 # Change that global x.
|
|
|
|
|
|
my_func1()
|
|
|
my_func2()
|
|
|
|
|
|
# Print global variable x.
|
|
|
print(x) # Did x get permanently changed by my_func2()?</code></pre>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="you-do-just-a-day-in-the-jungle" class="level2">
|
|
|
<h2>You Do: Just a Day in the Jungle</h2>
|
|
|
<p>Open a new local file, <code>piranhas.py</code>.</p>
|
|
|
<ul>
|
|
|
<li>Declare a global variable <code>piranhas_hungry</code> and set it to <code>True</code>.</li>
|
|
|
<li>Write two functions, <code>swing_vine_over_river</code> and <code>jump_in_river</code>.</li>
|
|
|
<li>In <code>swing_vine_over_river</code>, print <code>Ahhh! Piranhas got me!</code>.
|
|
|
<ul>
|
|
|
<li>Change <code>piranhas_hungry</code> to <code>False</code>.</li>
|
|
|
</ul></li>
|
|
|
<li>In <code>jump_in_river</code>, if <code>piranhas_hungry</code> is <code>True</code>, print <code>I'm not going in there! There are hungry piranhas!</code>.
|
|
|
<ul>
|
|
|
<li>Otherwise, print <code>Piranhas are full! Swimming happily through the Amazon!</code></li>
|
|
|
</ul></li>
|
|
|
</ul>
|
|
|
<div class="sourceCode" id="cb9"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb9-1" data-line-number="1"><span class="co"># Call functions in this order.</span></a>
|
|
|
<a class="sourceLine" id="cb9-2" data-line-number="2">jump_in_river()</a>
|
|
|
<a class="sourceLine" id="cb9-3" data-line-number="3">swing_vine_over_river()</a>
|
|
|
<a class="sourceLine" id="cb9-4" data-line-number="4">jump_in_river()</a></code></pre></div>
|
|
|
<p><strong>Pro tip:</strong> Raise your hand if you need some help!</p>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tip:</strong></p>
|
|
|
<ul>
|
|
|
<li>Give students a few minutes. The answer is in a repl.it on the next slide.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="we-do-check-your-answers" class="level2">
|
|
|
<h2>We Do: Check Your Answers</h2>
|
|
|
<ul>
|
|
|
<li>Did you remember the <code>global</code> keyword?</li>
|
|
|
<li>What happens if that keyword is removed?</li>
|
|
|
<li>Comment out line 4. What happens? Why?</li>
|
|
|
</ul>
|
|
|
<iframe height="400px" width="100%" src="https://repl.it/@SuperTernary/python-programming-scope-piranha?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals">
|
|
|
</iframe>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="summary-and-qa" class="level2">
|
|
|
<h2>Summary and Q&A</h2>
|
|
|
<p>Python checks <strong>scope</strong> to find the right variable.</p>
|
|
|
<ul>
|
|
|
<li>Functions and classes create individual <strong>local scopes</strong>.
|
|
|
<ul>
|
|
|
<li>A <code>local</code> variable doesn’t exist outside its local function or class scope.</li>
|
|
|
</ul></li>
|
|
|
<li>Any variable declared or assigned outside of any function or class is considered “global.”
|
|
|
<ul>
|
|
|
<li>Variables that are in <strong>global scope</strong> can be accessed anywhere.</li>
|
|
|
</ul></li>
|
|
|
</ul>
|
|
|
<p>Python will check for a <code>local</code> variable before using a <code>global</code> one.</p>
|
|
|
<p>There can be more levels. Python always works from the inside out — keep that in mind as your programs get more advanced!</p>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tip:</strong></p>
|
|
|
<ul>
|
|
|
<li>Do a check for understanding.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="additional-resources" class="level2">
|
|
|
<h2>Additional Resources</h2>
|
|
|
<ul>
|
|
|
<li><a href="https://www.python-course.eu/python3_global_vs_local_variables.php">Global vs. Local Variables</a></li>
|
|
|
<li><a href="http://python-textbok.readthedocs.io/en/1.0/Variables_and_Scope.html">Variables and Scope</a></li>
|
|
|
<li><a href="https://realpython.com/inner-functions-what-are-they-good-for/">Nested Functions — What Are They Good For?</a></li>
|
|
|
</ul>
|
|
|
</section>
|
|
|
|
|
|
</div>
|
|
|
<footer><span class='slide-number'></span></footer>
|
|
|
</div>
|
|
|
<script src="../../../../lib/js/head.min.js"></script>
|
|
|
<script src="../../../../js/reveal.js"></script>
|
|
|
|
|
|
<script>
|
|
|
|
|
|
var dependencies = [
|
|
|
{ src: '../../../../lib/js/classList.js', condition: function() { return !document.body.classList; } },
|
|
|
{ src: '../../../../plugin/markdown/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
|
|
|
{ src: '../../../../plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
|
|
|
{ src: '../../../../plugin/prism/prism.js', async: true, callback: function() { /*hljs.initHighlightingOnLoad();*/ } },
|
|
|
{ src: '../../../../plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } }
|
|
|
];
|
|
|
|
|
|
if (Reveal.getQueryHash().instructor === 1) {
|
|
|
dependencies.push({ src: '../../../../plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } });
|
|
|
}
|
|
|
// Full list of configuration options available here:
|
|
|
// https://github.com/hakimel/reveal.js#configuration
|
|
|
Reveal.initialize({
|
|
|
controls: true,
|
|
|
progress: true,
|
|
|
history: true,
|
|
|
center: false,
|
|
|
slideNumber: true,
|
|
|
|
|
|
// available themes are in /css/theme
|
|
|
theme: Reveal.getQueryHash().theme || 'default',
|
|
|
|
|
|
// default/cube/page/concave/zoom/linear/fade/none
|
|
|
transition: Reveal.getQueryHash().transition || 'slide',
|
|
|
|
|
|
// Optional libraries used to extend on reveal.js
|
|
|
dependencies: dependencies
|
|
|
});
|
|
|
|
|
|
if (Reveal.getQueryHash().instructor === 1) {
|
|
|
Reveal.configure(dependencies.push({ src: '../../../../plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }));
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Reveal.addEventListener('ready', function() {
|
|
|
if (Reveal.getCurrentSlide().classList.contains('separator-subhead')) {
|
|
|
document.getElementById('theme').setAttribute('href', '../../../../css/theme/ga-subhead.css');
|
|
|
} else if (Reveal.getCurrentSlide().classList.contains('separator')) {
|
|
|
document.getElementById('theme').setAttribute('href', '../../../../css/theme/ga-title.css')
|
|
|
} else {
|
|
|
document.getElementById('theme').setAttribute('href', '../../../../css/theme/ga.css');
|
|
|
}
|
|
|
});
|
|
|
|
|
|
Reveal.addEventListener('slidechanged', function(e) {
|
|
|
if (Reveal.getCurrentSlide().classList.contains('separator-subhead')) {
|
|
|
document.getElementById('theme').setAttribute('href', '../../../../css/theme/ga-subhead.css');
|
|
|
} else if (Reveal.getCurrentSlide().classList.contains('separator')) {
|
|
|
document.getElementById('theme').setAttribute('href', '../../../../css/theme/ga-title.css')
|
|
|
} else {
|
|
|
document.getElementById('theme').setAttribute('href', '../../../../css/theme/ga.css');
|
|
|
}
|
|
|
});
|
|
|
</script>
|
|
|
|
|
|
</body>
|
|
|
</html>
|