|
|
<!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">
|
|
|
|
|
|
|
|
|
<section id="section" class="level2 separator">
|
|
|
<h2><img src="https://s3.amazonaws.com/python-ga/images/GA_Cog_Medium_White_RGB.png" /></h2>
|
|
|
<h1>
|
|
|
Python Programming: Objects and Classes
|
|
|
</h1>
|
|
|
<!--
|
|
|
|
|
|
## Overview
|
|
|
This lesson walks through creating classes in Python. The lesson starts with We Dos for creating a class and instantiating objects from that class. It's followed by a We Do for class and instance variables. It ends with a few partner exercises, then a series of Knowledge Checks.
|
|
|
|
|
|
## Learning Objectives
|
|
|
In this lesson, students will:
|
|
|
|
|
|
- Define a class.
|
|
|
- Instantiate an object from a class.
|
|
|
- Create classes with class variables.
|
|
|
|
|
|
|
|
|
## Duration
|
|
|
50 MINUTES
|
|
|
|
|
|
## Suggested Agenda
|
|
|
|
|
|
| Time | Activity |
|
|
|
| --- | --- |
|
|
|
| 0:00 - 0:03 | Welcome |
|
|
|
| 0:03 - 0:10 | Class Overview |
|
|
|
| 0:10 - 0:25 | Creating Classes |
|
|
|
| 0:25 - 0:30 | Class Variables |
|
|
|
| 0:30 - 0:47 | Exercises |
|
|
|
| 0:47 - 0:50 | Summary |
|
|
|
|
|
|
## In Class: Materials
|
|
|
- Projector
|
|
|
- Internet connection
|
|
|
- Python 3
|
|
|
-->
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="learning-objectives" class="level2">
|
|
|
<h2>Learning Objectives</h2>
|
|
|
<p><em>After this lesson, you will be able to…</em></p>
|
|
|
<ul>
|
|
|
<li>Define a class.</li>
|
|
|
<li>Instantiate an object from a class.</li>
|
|
|
<li>Create classes with default instance variables.</li>
|
|
|
</ul>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="blueprints" class="level2">
|
|
|
<h2>Blueprints</h2>
|
|
|
<p>All cars have things that make them a <code>Car</code>. Although the details might be different, every type of car has the same basics — it’s off the same blueprint, with the same properties and actions.</p>
|
|
|
<p><img src="https://s3.amazonaws.com/ga-instruction/assets/programming-fundamentals/class-objects-car.png" /></p>
|
|
|
<ul>
|
|
|
<li>Property: A shape (could be hatchback or sedan).</li>
|
|
|
<li>Property: A color (could be red, black, blue, or silver).</li>
|
|
|
<li>Property: Seats (could be between 2 and 5).</li>
|
|
|
<li>Action: Can drive.</li>
|
|
|
<li>Action: Can park.</li>
|
|
|
</ul>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Talking Points:</strong></p>
|
|
|
<ul>
|
|
|
<li>When we make a car, we can vary the values of these properties. Some cars have economy engines, some have performance engines. Some have four doors, others have two. However, they are all types of <code>Car</code>s.</li>
|
|
|
</ul>
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>Point out the small “c” versus the big “C”!</li>
|
|
|
<li>Encourage students to think of other commonalities of cars — get them thinking about an archetypal car.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="introduction-objects-and-classes" class="level2">
|
|
|
<h2>Introduction: Objects and Classes</h2>
|
|
|
<p>These properties and behaviors can be thought of as variables and functions.</p>
|
|
|
<p><code>Car</code> blueprint:</p>
|
|
|
<ul>
|
|
|
<li>Properties (variables): <code>shape</code>, <code>color</code>, <code>seats</code></li>
|
|
|
<li>Actions (functions): <code>drive()</code> and <code>park()</code></li>
|
|
|
</ul>
|
|
|
<p>An actual car might have:</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="co"># **Properties - Variables**:</span></a>
|
|
|
<a class="sourceLine" id="cb1-2" data-line-number="2"><span class="op">-</span> shape <span class="op">=</span> <span class="st">"hatchback"</span></a>
|
|
|
<a class="sourceLine" id="cb1-3" data-line-number="3"><span class="op">-</span> color <span class="op">=</span> <span class="st">"red"</span>, <span class="st">"black"</span>, <span class="st">"blue"</span>, <span class="kw">or</span> <span class="st">"silver"</span></a>
|
|
|
<a class="sourceLine" id="cb1-4" data-line-number="4"><span class="op">-</span> seats <span class="op">=</span> <span class="dv">2</span></a>
|
|
|
<a class="sourceLine" id="cb1-5" data-line-number="5"></a>
|
|
|
<a class="sourceLine" id="cb1-6" data-line-number="6"><span class="co"># **Actions - Functions:**</span></a>
|
|
|
<a class="sourceLine" id="cb1-7" data-line-number="7"><span class="op">-</span> drive()</a>
|
|
|
<a class="sourceLine" id="cb1-8" data-line-number="8"><span class="op">-</span> park()</a>
|
|
|
<a class="sourceLine" id="cb1-9" data-line-number="9"><span class="op">-</span> reverse()</a></code></pre></div>
|
|
|
<p><strong>Discussion:</strong> What might a blueprint for a chair look like?</p>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>Give more examples on the board, both real-world and what students have seen.</li>
|
|
|
<li>Get students to where they are able to define the concept of a class. Can they come up with examples, like the chair? (Feel free to change the chair to something else!)</li>
|
|
|
</ul>
|
|
|
<p><strong>Talking Points:</strong></p>
|
|
|
<ul>
|
|
|
<li>Every day, we interact with objects like chairs, beverages, cars, other people, etc. These objects have properties that define them and behaviors we can execute to interact with them.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="discussion-python-classes" class="level2">
|
|
|
<h2>Discussion: Python Classes</h2>
|
|
|
<p>In Python, the concept of blueprints and objects is common. A <strong>class</strong> is the blueprint for an <strong>object</strong>. Everything you declare in Python is an object, and it has a class.</p>
|
|
|
<p>Consider the <code>List</code> class — every list you make has the same basic concept.</p>
|
|
|
<p>Variables:</p>
|
|
|
<ul>
|
|
|
<li>Elements: What’s in the list! E.g., <code>my_list = [element1, element2, element3]</code>.</li>
|
|
|
</ul>
|
|
|
<p>Functions that all lists have:</p>
|
|
|
<ul>
|
|
|
<li><code>my_list.pop()</code>, <code>my_list.append()</code>, <code>my.list.insert(index)</code></li>
|
|
|
</ul>
|
|
|
<p><img src="https://s3.amazonaws.com/ga-instruction/assets/programming-fundamentals/pythonlistclass.png" /></p>
|
|
|
<p>What behaviors and properties do you think are in the <code>Dictionary</code> class? The <code>Set</code> class?</p>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>This is a tough slide, so spend a few minutes to make sure students get it.</li>
|
|
|
<li>Have a discussion! Write on the board and have students brainstorm what’s common about dictionaries. This doubles as a dictionary and set review.</li>
|
|
|
<li>If you think it will help, pull up the <code>List</code> class specification — although this might be better to do after students learn the class syntax.</li>
|
|
|
</ul>
|
|
|
<p><strong>Talking Points:</strong></p>
|
|
|
<ul>
|
|
|
<li>Similarly, you will hear people say that “everything is an object” in Python. Python is an object-oriented programming language.</li>
|
|
|
<li>What this means is that nearly every variable you declare actually has a set of properties and functions that it can use — it is an object.</li>
|
|
|
<li>Every string, number, list, dictionary, etc. has a set of behaviors and properties that are “baked in” because they are instances of a class.</li>
|
|
|
<li>Every list you declare has properties (the values in it) and behaviors — functions like <code>append()</code> and <code>pop()</code>.</li>
|
|
|
<li>In Python, there is a built-in <code>List</code> class, which has the ability to hold values, and built-in list functions like <code>append()</code> and <code>pop()</code>. When you declare a list in Python, you’re making your own list object that’s a variation of the <code>List</code> class.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="discussion-a-dog-class" class="level2">
|
|
|
<h2>Discussion: A <code>Dog</code> Class</h2>
|
|
|
<p>We can make a class for anything! Let’s create a <code>Dog</code> class.</p>
|
|
|
<p>The objects might be <code>greyhound</code>, <code>goldenRetriever</code>, <code>corgi</code>, etc.</p>
|
|
|
<p>Think about the <code>Dog</code> blueprint. What variables might our class have? What functions?</p>
|
|
|
<p><img src="https://s3.amazonaws.com/ga-instruction/assets/programming-fundamentals/class-dog-blueprint.png" /></p>
|
|
|
<p><strong>Pro tip:</strong> When functions are in a class, they are called “methods.” They’re the same thing!</p>
|
|
|
<p><strong>Pro tip:</strong> While objects are named in snake_case, classes are conventionally named in TitleCase.</p>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>Try to lead students toward name, age, color, and barking — that’s what will be in the class we create.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="we-do-defining-classes" class="level2">
|
|
|
<h2>We Do: Defining Classes</h2>
|
|
|
<p>Follow along! Let’s create a new file, <code>Dog.py</code>.</p>
|
|
|
<p>Class definitions are similar to function definitions, but instead of <code>def</code>, we use <code>class</code>.</p>
|
|
|
<p>Let’s declare a class for <code>Dog</code>:</p>
|
|
|
<div class="sourceCode" id="cb2"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb2-1" data-line-number="1"><span class="kw">class</span> Dog:</a>
|
|
|
<a class="sourceLine" id="cb2-2" data-line-number="2"> <span class="co"># We'll define the class here.</span></a>
|
|
|
<a class="sourceLine" id="cb2-3" data-line-number="3"> <span class="co"># Our dog will have two variables: name and age.</span></a></code></pre></div>
|
|
|
<p><img src="https://s3.amazonaws.com/ga-instruction/assets/programming-fundamentals/class-dog-name-age.png" /></p>
|
|
|
<p><strong>Pro tip:</strong> Files are usually named for their class, so the <code>Dog</code> class is in <code>Dog.py</code>.</p>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>Make sure students do this with you! Practicing syntax is key.</li>
|
|
|
</ul>
|
|
|
<p><strong>Talking Points:</strong></p>
|
|
|
<ul>
|
|
|
<li>It’s very common in programming to make our own classes to organize data in the ways we want. Python gives us, for example, a string and an integer.</li>
|
|
|
<li>What if we want to store a bunch of data where each item contains a string <strong>and</strong> an integer <strong>and</strong> a function that prints them out nicely?</li>
|
|
|
<li>We can make a class for that, and then each object we make from that class will have that all baked in.</li>
|
|
|
<li>Note that we can <strong>optionally</strong> (as of Python3) include parentheses after our class declaration, i.e. <code>class Dog:</code></li>
|
|
|
<li>All classes, if no explicit inheritance is given, will inherit from the <code>Object</code> class (the parent of all Python classes)</li>
|
|
|
<li>The base class can be shown by typing <code><your_class-name>.__bases__</code>. If you haven’t specified a parent, it should return <code>object</code></li>
|
|
|
<li><code>class Dog:</code> is functionally identical to <code>class Dog:</code> and <code>class Dog(object):</code>.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="we-do-adding-docstrings-part-1" class="level2">
|
|
|
<h2>We Do: Adding Docstrings, Part 1</h2>
|
|
|
<p>Using our <code>Dog</code> class from the previous exercise, let’s add a <em>docstring</em>.</p>
|
|
|
<p>A <a href="https://www.python.org/dev/peps/pep-0257/#what-is-a-docstring">docstring</a> is a comment that is placed at the top of a class (or function). When python precompiles the class or function (which happens in the background), it parses this comment and makes it available to the user as a helpfile when the class is accessed when typing the class or function, followed by a <code>?</code> question mark. In a Jupyter Notebook, this can also be accessed by placing your cursor at the end of a class or function and pressing <code>SHIFT + TAB</code>. Try it out! Enter <code>list?</code> in a python interpreter or Jupyter cell and run it. What is returned?</p>
|
|
|
<div class="sourceCode" id="cb3"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb3-1" data-line-number="1">In [<span class="dv">1</span>]: <span class="bu">list</span>? </a>
|
|
|
<a class="sourceLine" id="cb3-2" data-line-number="2">Init signature: <span class="bu">list</span>(<span class="va">self</span>, <span class="op">/</span>, <span class="op">*</span>args, <span class="op">**</span>kwargs)</a>
|
|
|
<a class="sourceLine" id="cb3-3" data-line-number="3">Docstring: </a>
|
|
|
<a class="sourceLine" id="cb3-4" data-line-number="4"><span class="bu">list</span>() <span class="op">-></span> new empty <span class="bu">list</span></a>
|
|
|
<a class="sourceLine" id="cb3-5" data-line-number="5"><span class="bu">list</span>(iterable) <span class="op">-></span> new <span class="bu">list</span> initialized <span class="im">from</span> iterable<span class="st">'s items</span></a>
|
|
|
<a class="sourceLine" id="cb3-6" data-line-number="6"><span class="st">Type: type</span></a></code></pre></div>
|
|
|
<p>Here, we can see under the <code>Docstring</code> section are the ‘instructions for use’, and the <code>Init signature</code> is a list of arguments that can be passed to the function, which is automatically recognized and generated by the python compiler.</p>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips</strong>:</p>
|
|
|
<ul>
|
|
|
<li><p>Give the students a minute to try out getting the docstring on their favorite function or class. Here, we’re using a builtin function, but it’s also very cool to show the <code>.__path__</code> and <code>.__doc__</code> private class variables, and where the actual docstring is in a real class like a pandas library.</p></li>
|
|
|
<li><p>Direct students to the best practices within <a href="https://www.python.org/dev/peps/pep-0257/">PEP-0257</a> for creating docstrings. There’s a lot to know, more than we have time to cover.</p></li>
|
|
|
<li><p>Encourage them to use the PEP syntax reference in the future for other things to make sure their code is standardized and adheres to the <a href="https://en.wikipedia.org/wiki/Principle_of_least_astonishment">principle of least astonishment</a>.</p></li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="we-do-adding-docstrings-part-2" class="level2">
|
|
|
<h2>We Do: Adding Docstrings, Part 2</h2>
|
|
|
<p>Using our <code>Dog</code> class from the previous exercise, let’s add a <em>docstring</em>.</p>
|
|
|
<p>The docstring for a function or method should summarize its behavior and document its arguments, return value(s), side effects, exceptions raised, and restrictions on when it can be called. Not all of these will be applicable for every function or method, so the programmer should take care to document the parts relevant. Optional arguments should be indicated. It should be documented whether keyword arguments are part of the interface.</p>
|
|
|
<p>The first line is a simple description of the class or function, followed by a blank line, followed by keyword arguments, if any.</p>
|
|
|
<p>Let’s write a docstring for <code>Dog</code>:</p>
|
|
|
<div class="sourceCode" id="cb4"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb4-1" data-line-number="1"><span class="kw">class</span> Dog:</a>
|
|
|
<a class="sourceLine" id="cb4-2" data-line-number="2"> <span class="co">"""Creates Dog class, possible child class of Animal</span></a>
|
|
|
<a class="sourceLine" id="cb4-3" data-line-number="3"></a>
|
|
|
<a class="sourceLine" id="cb4-4" data-line-number="4"><span class="co"> Parameters</span></a>
|
|
|
<a class="sourceLine" id="cb4-5" data-line-number="5"><span class="co"> ----------</span></a>
|
|
|
<a class="sourceLine" id="cb4-6" data-line-number="6"><span class="co"> name : str, default blank</span></a>
|
|
|
<a class="sourceLine" id="cb4-7" data-line-number="7"><span class="co"> Desired name of Dog</span></a>
|
|
|
<a class="sourceLine" id="cb4-8" data-line-number="8"><span class="co"> age : int, default 0</span></a>
|
|
|
<a class="sourceLine" id="cb4-9" data-line-number="9"><span class="co"> Age of Dog in years</span></a>
|
|
|
<a class="sourceLine" id="cb4-10" data-line-number="10"><span class="co"> """</span></a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>Note that we are defining the name and age parameters ahead of time. We haven’t created them just yet but we will in the next few steps.</li>
|
|
|
</ul>
|
|
|
<p><strong>Talking Points:</strong></p>
|
|
|
<ul>
|
|
|
<li>Also what is usually included in a class docstring is a ‘see also’ section, for similar classes, as well as an ‘example’ section that shows how the class is instantiated and used with some common use cases. This was omitted for brevity.</li>
|
|
|
<li>An important part of this docstring is the <code>type</code> of the parameter for each parameter. This tells the user how to input data into the class. For example, we <em>could have</em> inserted our age as a string, but we are expecting an int. Since python is not strongly typed, it is important to tell the user of your code what types of variables should be passed.</li>
|
|
|
<li>Also important for methods is the <code>return type</code> of the method (if any). This will let the user know if they need to ‘catch’ any object that is returned from the method into a variable.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="we-do-the-__init__-method" class="level2">
|
|
|
<h2>We Do: The <code>__init__</code> Method</h2>
|
|
|
<p>What first? Every class starts with an <code>__init__</code> method. It’s:</p>
|
|
|
<ul>
|
|
|
<li>Where we define the class’ variables.</li>
|
|
|
<li>Short for “initialize.”
|
|
|
<ul>
|
|
|
<li>“Every time you make an object from this class, do what’s in here.”</li>
|
|
|
</ul></li>
|
|
|
</ul>
|
|
|
<p>Let’s add this:</p>
|
|
|
<div class="sourceCode" id="cb5"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb5-1" data-line-number="1"><span class="kw">class</span> Dog:</a>
|
|
|
<a class="sourceLine" id="cb5-2" data-line-number="2"> <span class="co">"""Creates Dog class, possible child class of Animal</span></a>
|
|
|
<a class="sourceLine" id="cb5-3" data-line-number="3"></a>
|
|
|
<a class="sourceLine" id="cb5-4" data-line-number="4"><span class="co"> Parameters</span></a>
|
|
|
<a class="sourceLine" id="cb5-5" data-line-number="5"><span class="co"> ----------</span></a>
|
|
|
<a class="sourceLine" id="cb5-6" data-line-number="6"><span class="co"> name : str, default blank</span></a>
|
|
|
<a class="sourceLine" id="cb5-7" data-line-number="7"><span class="co"> Desired name of Dog</span></a>
|
|
|
<a class="sourceLine" id="cb5-8" data-line-number="8"><span class="co"> age : int, default 0</span></a>
|
|
|
<a class="sourceLine" id="cb5-9" data-line-number="9"><span class="co"> Age of Dog in years</span></a>
|
|
|
<a class="sourceLine" id="cb5-10" data-line-number="10"><span class="co"> """</span></a>
|
|
|
<a class="sourceLine" id="cb5-11" data-line-number="11"></a>
|
|
|
<a class="sourceLine" id="cb5-12" data-line-number="12"> <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>, name<span class="op">=</span><span class="st">""</span>, age<span class="op">=</span><span class="dv">0</span>):</a>
|
|
|
<a class="sourceLine" id="cb5-13" data-line-number="13"> <span class="co"># Note the optional parameters and defaults.</span></a>
|
|
|
<a class="sourceLine" id="cb5-14" data-line-number="14"> <span class="va">self</span>.name <span class="op">=</span> name <span class="co"># All dogs have a name.</span></a>
|
|
|
<a class="sourceLine" id="cb5-15" data-line-number="15"> <span class="va">self</span>.age <span class="op">=</span> age <span class="co"># All dogs have an age.</span></a></code></pre></div>
|
|
|
<p><em>Note: <code>self</code> means “each individual object made from this class.” Not every “dog” has the same name!</em> <em>Note: in the effort of brevity, we wil be omitting the docstring from future examples!</em></p>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li><code>Self</code> is tough! It will make more sense after you start creating objects. Don’t spend more than a few minutes on it — go back to it when comparing instance versus class variables.</li>
|
|
|
<li>Call out the default values, or optional parameters, as we very recently learned them.</li>
|
|
|
<li>Method means function!</li>
|
|
|
<li>Be sure that students are aware that we are omitting the docstring from future examples to keep the code short and concise. They will be expected to have docstrings on classes and methods for their homework! Documentation is critical!</li>
|
|
|
</ul>
|
|
|
<p><strong>Talking Points:</strong></p>
|
|
|
<ul>
|
|
|
<li>When we make an object, we’ll first set its variables.</li>
|
|
|
<li>The first argument passed to the <code>__init__</code> function, <code>self</code>, is required when defining methods for classes. The <code>self</code> argument is a reference to a future instantiation of the class. In other words, <code>self</code> refers to each individual dog.</li>
|
|
|
<li>This lets each object made from a class keep references to its own data and function members. Not every “dog” has the same attributes, so we want individual cars to maintain their own attributes.</li>
|
|
|
<li>Python allows us to provide default values for parameters in any function we provide. Here, if no <code>name</code> or <code>age</code> values are provided when a <code>Dog</code> is initialized, they will default. To create default values, we assign values to the parameter <code>capacity</code> <em>inside</em> the parentheses. You’ve seen this!</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="we-do-adding-a-bark_hello-method" class="level2">
|
|
|
<h2>We Do: Adding a <code>bark_hello()</code> Method</h2>
|
|
|
<p>All dogs have the behavior <code>bark</code>, so let’s add that. This is a regular function (method), just inside the class!</p>
|
|
|
<div class="sourceCode" id="cb6"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb6-1" data-line-number="1"><span class="kw">class</span> Dog:</a>
|
|
|
<a class="sourceLine" id="cb6-2" data-line-number="2"></a>
|
|
|
<a class="sourceLine" id="cb6-3" data-line-number="3"> <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>, name<span class="op">=</span><span class="st">""</span>, age<span class="op">=</span><span class="dv">0</span>):</a>
|
|
|
<a class="sourceLine" id="cb6-4" data-line-number="4"> <span class="co"># Note the defaults.</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="va">self</span>.name <span class="op">=</span> name <span class="co"># All dogs have a name.</span></a>
|
|
|
<a class="sourceLine" id="cb6-7" data-line-number="7"> <span class="va">self</span>.age <span class="op">=</span> age <span class="co"># All dogs have an age.</span></a>
|
|
|
<a class="sourceLine" id="cb6-8" data-line-number="8"></a>
|
|
|
<a class="sourceLine" id="cb6-9" data-line-number="9"> <span class="co"># All dogs have a bark function.</span></a>
|
|
|
<a class="sourceLine" id="cb6-10" data-line-number="10"> <span class="co">"""Prints a message stating name and age to stdout"""</span></a>
|
|
|
<a class="sourceLine" id="cb6-11" data-line-number="11"> <span class="kw">def</span> bark_hello(<span class="va">self</span>):</a>
|
|
|
<a class="sourceLine" id="cb6-12" data-line-number="12"> <span class="bu">print</span>(<span class="st">"Woof! I am called"</span>, <span class="va">self</span>.name, <span class="st">"; I am"</span>, <span class="va">self</span>.age, <span class="st">"human-years old."</span>)</a></code></pre></div>
|
|
|
<p>We’re done defining the class!</p>
|
|
|
<p><em>Note: We have also added a docstring to our new <code>.bark_hello()</code> method. This will be omitted for future examples for brevity.</em></p>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>Note that it takes <code>self</code>! This will be tough to remember. Point out that it matches how the variable was declared.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="aside-instantiating-objects-from-classes" class="level2">
|
|
|
<h2>Aside: Instantiating Objects From Classes</h2>
|
|
|
<p>Now we have a <code>Dog</code> template!</p>
|
|
|
<p>Each <code>dog</code> object we make from this template:</p>
|
|
|
<ul>
|
|
|
<li>Has a name.</li>
|
|
|
<li>Has an age.</li>
|
|
|
<li>Can bark.</li>
|
|
|
</ul>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>Point out the lowercase “d!”</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="we-do-how-do-we-make-a-dog-object" class="level2">
|
|
|
<h2>We Do: How Do We Make a <code>Dog</code> Object?</h2>
|
|
|
<p>We call our class name like we call a function — passing in arguments, which go to the <code>init</code>.</p>
|
|
|
<p>Add this under your class (non-indented!):</p>
|
|
|
<div class="sourceCode" id="cb7"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb7-1" data-line-number="1"><span class="co"># Declare the objects.</span></a>
|
|
|
<a class="sourceLine" id="cb7-2" data-line-number="2">gracie <span class="op">=</span> Dog(<span class="st">"Gracie"</span>, <span class="dv">8</span>)</a>
|
|
|
<a class="sourceLine" id="cb7-3" data-line-number="3">spitz <span class="op">=</span> Dog(<span class="st">"Spitz"</span>, <span class="dv">5</span>)</a>
|
|
|
<a class="sourceLine" id="cb7-4" data-line-number="4">buck <span class="op">=</span> Dog(<span class="st">"Buck"</span>, <span class="dv">3</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="co"># Test them out!</span></a>
|
|
|
<a class="sourceLine" id="cb7-7" data-line-number="7">gracie.bark_hello()</a>
|
|
|
<a class="sourceLine" id="cb7-8" data-line-number="8"><span class="bu">print</span>(<span class="st">"This dog's name is"</span>, gracie.name)</a>
|
|
|
<a class="sourceLine" id="cb7-9" data-line-number="9"><span class="bu">print</span>(<span class="st">"This dog's age is"</span>, gracie.age)</a>
|
|
|
<a class="sourceLine" id="cb7-10" data-line-number="10">spitz.bark_hello()</a>
|
|
|
<a class="sourceLine" id="cb7-11" data-line-number="11">buck.bark_hello()</a></code></pre></div>
|
|
|
<p>Try it! Run <code>Dog.py</code> like a normal Python file: <code>python Dog.py</code>.</p>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>Map out that these variables go to <code>__init__</code> to get created. All arguments go to <code>__init__</code>!</li>
|
|
|
<li>They have a working class! Go back through the whole code and make sure students understand.</li>
|
|
|
<li>After running this, stop and check for understanding.</li>
|
|
|
</ul>
|
|
|
<p><strong>Talking Points:</strong></p>
|
|
|
<ul>
|
|
|
<li>This will create a new object according to our <code>Dog</code> class specification.</li>
|
|
|
<li>Python runs our <code>__init__</code> method to initialize the object.</li>
|
|
|
<li>Here, we are telling our <code>__init__</code> method to set the name of this <code>dog</code> to <code>'Gracie'</code> and set her age to <code>8</code> years old.</li>
|
|
|
<li>Even though <code>self</code> is listed as a parameter for the <code>bark_hello()</code> function, we don’t pass it into the function. It happens automatically.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="we-do-adding-print" class="level2">
|
|
|
<h2>We Do: Adding Print</h2>
|
|
|
<p><code>__init__</code> is just a method. It creates variables, but we can also add a <code>print</code> statement! This will run when we create the object.</p>
|
|
|
<div class="sourceCode" id="cb8"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb8-1" data-line-number="1"><span class="kw">class</span> Dog:</a>
|
|
|
<a class="sourceLine" id="cb8-2" data-line-number="2"> <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>, name<span class="op">=</span><span class="st">""</span>, age<span class="op">=</span><span class="dv">0</span>):</a>
|
|
|
<a class="sourceLine" id="cb8-3" data-line-number="3"> <span class="va">self</span>.name <span class="op">=</span> name</a>
|
|
|
<a class="sourceLine" id="cb8-4" data-line-number="4"> <span class="va">self</span>.age <span class="op">=</span> age</a>
|
|
|
<a class="sourceLine" id="cb8-5" data-line-number="5"> <span class="bu">print</span>(name, <span class="st">"created."</span>) <span class="co"># Run when init is finished.</span></a>
|
|
|
<a class="sourceLine" id="cb8-6" data-line-number="6"></a>
|
|
|
<a class="sourceLine" id="cb8-7" data-line-number="7"> <span class="kw">def</span> bark_hello(<span class="va">self</span>):</a>
|
|
|
<a class="sourceLine" id="cb8-8" data-line-number="8"> <span class="bu">print</span>(<span class="st">"Woof! I am called"</span>, <span class="va">self</span>.name, <span class="st">"; I am"</span>, <span class="va">self</span>.age, <span class="st">"human-years old."</span>)</a>
|
|
|
<a class="sourceLine" id="cb8-9" data-line-number="9"></a>
|
|
|
<a class="sourceLine" id="cb8-10" data-line-number="10">fox <span class="op">=</span> Dog(<span class="st">"Fox"</span>) <span class="co"># Note that "Fox created." prints — and we're using the default age.</span></a>
|
|
|
<a class="sourceLine" id="cb8-11" data-line-number="11">fox.bark_hello()</a></code></pre></div>
|
|
|
<p>Try it!</p>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>Run this a few times to show the default variables. Take out the default for name to show that variables don’t need a default value.</li>
|
|
|
</ul>
|
|
|
<p><strong>Talking Points:</strong></p>
|
|
|
<ul>
|
|
|
<li>Reminder: “Method” is a function in a class.</li>
|
|
|
<li>Note that <code>print</code> statements can be anywhere — it’s not a variable! <code>__init__</code> is a method.</li>
|
|
|
<li>The <code>__init__</code> method will execute once and only once when you create a new object from a class. Note that the <code>print</code> statement never happens again.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="quick-review-classes" class="level2">
|
|
|
<h2>Quick Review: Classes</h2>
|
|
|
<p>A class is a blueprint for an object. Some classes are built into Python, like <code>List</code>. We can always make a <code>list</code> object.</p>
|
|
|
<p>We can make a class for anything!</p>
|
|
|
<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"># Created like a function; TitleCase</span></a>
|
|
|
<a class="sourceLine" id="cb9-2" data-line-number="2"><span class="kw">class</span> Dog:</a>
|
|
|
<a class="sourceLine" id="cb9-3" data-line-number="3"></a>
|
|
|
<a class="sourceLine" id="cb9-4" data-line-number="4"> <span class="co"># __init__: A method (function) that happens just once, when the object is created.</span></a>
|
|
|
<a class="sourceLine" id="cb9-5" data-line-number="5"> <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>, name<span class="op">=</span><span class="st">""</span>, age<span class="op">=</span><span class="dv">0</span>): <span class="co"># What's passed in to the class is used here.</span></a>
|
|
|
<a class="sourceLine" id="cb9-6" data-line-number="6"> <span class="co"># Set variables for each.</span></a>
|
|
|
<a class="sourceLine" id="cb9-7" data-line-number="7"> <span class="va">self</span>.name <span class="op">=</span> name</a>
|
|
|
<a class="sourceLine" id="cb9-8" data-line-number="8"> <span class="va">self</span>.age <span class="op">=</span> age</a>
|
|
|
<a class="sourceLine" id="cb9-9" data-line-number="9"> <span class="bu">print</span>(name, <span class="st">"created."</span>) <span class="co"># This will run when the __init__ method is called.</span></a>
|
|
|
<a class="sourceLine" id="cb9-10" data-line-number="10"></a>
|
|
|
<a class="sourceLine" id="cb9-11" data-line-number="11"> <span class="co"># Classes can have as many methods (functions) as you'd like.</span></a>
|
|
|
<a class="sourceLine" id="cb9-12" data-line-number="12"> <span class="kw">def</span> bark_hello(<span class="va">self</span>):</a>
|
|
|
<a class="sourceLine" id="cb9-13" data-line-number="13"> <span class="bu">print</span>(<span class="st">"Woof! I am called"</span>, <span class="va">self</span>.name, <span class="st">"; I am"</span>, <span class="va">self</span>.age, <span class="st">"human-years old."</span>)</a>
|
|
|
<a class="sourceLine" id="cb9-14" data-line-number="14"></a>
|
|
|
<a class="sourceLine" id="cb9-15" data-line-number="15">fox <span class="op">=</span> Dog(<span class="st">"Fox"</span>) <span class="co"># Creating the object calls __init__. Objects are snake_case.</span></a>
|
|
|
<a class="sourceLine" id="cb9-16" data-line-number="16"><span class="bu">print</span>(<span class="st">"This dog's name is"</span>, fox.name) <span class="co"># The object now has those variables!</span></a>
|
|
|
<a class="sourceLine" id="cb9-17" data-line-number="17">fox.bark_hello() <span class="co"># The object now has those methods and variables!</span></a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>Add your own reasons for why classes are useful.</li>
|
|
|
<li>Do a quick check for understanding.</li>
|
|
|
<li>Run down the comments in the code like they’re bullet points!</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="discussion-what-about-tea" class="level2">
|
|
|
<h2>Discussion: What About Tea?</h2>
|
|
|
<p>Let’s make a <code>TeaCup</code> class.</p>
|
|
|
<ul>
|
|
|
<li>What variables would a cup of tea have?</li>
|
|
|
<li>What methods?</li>
|
|
|
</ul>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>Have the students discuss suggestions for properties for a <code>Tea</code> class.</li>
|
|
|
<li>The exercise has <code>capacity</code>, <code>amount</code>, <code>fill</code>, <code>empty</code>, and <code>drink</code>.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="a-potential-teacup-class" class="level2">
|
|
|
<h2>A Potential <code>TeaCup</code> Class</h2>
|
|
|
<p>We could say:</p>
|
|
|
<p>Variables:</p>
|
|
|
<ul>
|
|
|
<li>A total <code>capacity</code>.</li>
|
|
|
<li>A current <code>amount</code>.</li>
|
|
|
</ul>
|
|
|
<p>Methods:</p>
|
|
|
<ul>
|
|
|
<li><code>fill()</code> our cup.</li>
|
|
|
<li><code>empty()</code> our cup.</li>
|
|
|
<li><code>drink()</code> some tea from our cup.</li>
|
|
|
</ul>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Talking Points:</strong></p>
|
|
|
<ul>
|
|
|
<li>Given a well-defined cup of tea, we can use the class definition to create <strong>instances</strong> of the class.</li>
|
|
|
<li>Each <strong>instance</strong> of the <code>TeaCup</code> class can have a different <code>capacity</code> and keep track of different <code>amounts</code>.</li>
|
|
|
<li>Although different, properties are affected by actions like <code>fill()</code>, <code>empty()</code>, and <code>drink()</code> similarly.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="example-a-teacup-class" class="level2">
|
|
|
<h2>Example: A <code>TeaCup</code> Class</h2>
|
|
|
<p>Here’s what a <code>TeaCup</code> class definition might look like in Python:</p>
|
|
|
<div class="sourceCode" id="cb10"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb10-1" data-line-number="1"><span class="kw">class</span> TeaCup:</a>
|
|
|
<a class="sourceLine" id="cb10-2" data-line-number="2"> <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>, capacity):</a>
|
|
|
<a class="sourceLine" id="cb10-3" data-line-number="3"> <span class="co"># Python executes when a new cup of tea is created.</span></a>
|
|
|
<a class="sourceLine" id="cb10-4" data-line-number="4"> <span class="va">self</span>.capacity <span class="op">=</span> capacity <span class="co"># Total ounces the cup holds.</span></a>
|
|
|
<a class="sourceLine" id="cb10-5" data-line-number="5"> <span class="va">self</span>.amount <span class="op">=</span> <span class="dv">0</span> <span class="co"># Current ounces in the cup. All cups start empty!</span></a>
|
|
|
<a class="sourceLine" id="cb10-6" data-line-number="6"></a>
|
|
|
<a class="sourceLine" id="cb10-7" data-line-number="7"> <span class="kw">def</span> fill(<span class="va">self</span>):</a>
|
|
|
<a class="sourceLine" id="cb10-8" data-line-number="8"> <span class="va">self</span>.amount <span class="op">=</span> <span class="va">self</span>.capacity</a>
|
|
|
<a class="sourceLine" id="cb10-9" data-line-number="9"></a>
|
|
|
<a class="sourceLine" id="cb10-10" data-line-number="10"> <span class="kw">def</span> empty(<span class="va">self</span>):</a>
|
|
|
<a class="sourceLine" id="cb10-11" data-line-number="11"> <span class="va">self</span>.amount <span class="op">=</span> <span class="dv">0</span></a>
|
|
|
<a class="sourceLine" id="cb10-12" data-line-number="12"></a>
|
|
|
<a class="sourceLine" id="cb10-13" data-line-number="13"> <span class="kw">def</span> drink(<span class="va">self</span>, amount_drank):</a>
|
|
|
<a class="sourceLine" id="cb10-14" data-line-number="14"> <span class="va">self</span>.amount <span class="op">-=</span> amount_drank</a>
|
|
|
<a class="sourceLine" id="cb10-15" data-line-number="15"> <span class="co"># If it's empty, it stays empty!</span></a>
|
|
|
<a class="sourceLine" id="cb10-16" data-line-number="16"> <span class="cf">if</span> (<span class="va">self</span>.amount <span class="op">==</span> <span class="dv">0</span>):</a>
|
|
|
<a class="sourceLine" id="cb10-17" data-line-number="17"> <span class="va">self</span>.amount <span class="op">=</span> <span class="dv">0</span></a>
|
|
|
<a class="sourceLine" id="cb10-18" data-line-number="18"></a>
|
|
|
<a class="sourceLine" id="cb10-19" data-line-number="19">steves_cup <span class="op">=</span> TeaCup(<span class="dv">12</span>) <span class="co"># Maybe a fancy tea latte.</span></a>
|
|
|
<a class="sourceLine" id="cb10-20" data-line-number="20">yis_cup <span class="op">=</span> TeaCup(<span class="dv">16</span>) <span class="co"># It's a rough morning!</span></a>
|
|
|
<a class="sourceLine" id="cb10-21" data-line-number="21">brandis_cup <span class="op">=</span> TeaCup(<span class="dv">2</span>) <span class="co"># Just a quick sip.</span></a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>This is the only slide, so just a demo. Talk through each line again to remind students what it does. Copy this to a file and run it!</li>
|
|
|
<li>This class doesn’t take in <code>amount</code>! It’s set as a variable, but not passed in. Call it out and explain.</li>
|
|
|
<li>Remind students that the object declarations have to go below the class. Show the error that occurs if you try calling <code>steves_cup</code> first.</li>
|
|
|
<li>Call each function — <code>fill()</code>, <code>drink()</code> with a different number for each, and printing the amount left with <code>cup.amount</code>.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="quick-knowledge-check" class="level2">
|
|
|
<h2>Quick Knowledge Check:</h2>
|
|
|
<div class="sourceCode" id="cb11"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb11-1" data-line-number="1"><span class="kw">class</span> TeaCup:</a>
|
|
|
<a class="sourceLine" id="cb11-2" data-line-number="2"> <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>, capacity <span class="op">=</span> <span class="dv">8</span>):</a>
|
|
|
<a class="sourceLine" id="cb11-3" data-line-number="3"> <span class="va">self</span>.capacity <span class="op">=</span> capacity</a>
|
|
|
<a class="sourceLine" id="cb11-4" data-line-number="4"> <span class="va">self</span>.amount <span class="op">=</span> <span class="dv">0</span></a></code></pre></div>
|
|
|
<p>When will the capacity be <code>8</code>?</p>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Talking Points:</strong></p>
|
|
|
<ul>
|
|
|
<li>See if students can answer this.</li>
|
|
|
<li>Answer: When no capacity is passed in when it’s declared.</li>
|
|
|
<li>After giving the answer, demo it!</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="variables-for-all-class-objects" class="level2">
|
|
|
<h2>Variables for All Class Objects</h2>
|
|
|
<p>Next up: new types of class variables!</p>
|
|
|
<p>Let’s revisit our <code>Dog</code> class:</p>
|
|
|
<div class="sourceCode" id="cb12"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb12-1" data-line-number="1"><span class="kw">class</span> Dog:</a>
|
|
|
<a class="sourceLine" id="cb12-2" data-line-number="2"></a>
|
|
|
<a class="sourceLine" id="cb12-3" data-line-number="3"> <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>, name<span class="op">=</span><span class="st">""</span>, age<span class="op">=</span><span class="dv">0</span>):</a>
|
|
|
<a class="sourceLine" id="cb12-4" data-line-number="4"> <span class="va">self</span>.name <span class="op">=</span> name</a>
|
|
|
<a class="sourceLine" id="cb12-5" data-line-number="5"> <span class="va">self</span>.age <span class="op">=</span> age</a>
|
|
|
<a class="sourceLine" id="cb12-6" data-line-number="6"> <span class="bu">print</span>(name, <span class="st">"created."</span>)</a>
|
|
|
<a class="sourceLine" id="cb12-7" data-line-number="7"></a>
|
|
|
<a class="sourceLine" id="cb12-8" data-line-number="8"> <span class="kw">def</span> bark_hello(<span class="va">self</span>):</a>
|
|
|
<a class="sourceLine" id="cb12-9" data-line-number="9"> <span class="bu">print</span>(<span class="st">"Woof! I am called"</span>, <span class="va">self</span>.name, <span class="st">"; I am"</span>, <span class="va">self</span>.age, <span class="st">"human-years old"</span>)</a></code></pre></div>
|
|
|
<p>What if there are variables that we want across all dogs?</p>
|
|
|
<p>For example, can we count how many <code>dog</code> objects we make and track it in the class?</p>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Talking Points:</strong></p>
|
|
|
<ul>
|
|
|
<li>Our <code>Dog</code> class had variables attached to <code>self</code> that exist independently for each object that’s created.
|
|
|
<ul>
|
|
|
<li>Each object instance has its own copies of these variables, and they can vary across objects.</li>
|
|
|
</ul></li>
|
|
|
<li>We can attach variables to the class itself so that there’s one single thing that exists for an entire class.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="i-do-class-vs.instance-members" class="level2">
|
|
|
<h2>I Do: Class vs. Instance Members</h2>
|
|
|
<p>We already have <strong>instance variables</strong>, which are specific to each <code>dog</code> object (each has its own name!).</p>
|
|
|
<p>A <strong>class variable</strong> is specific to the class, regardless of the object. It’s created <strong>above</strong> <code>__init__</code>.</p>
|
|
|
<div class="sourceCode" id="cb13"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb13-1" data-line-number="1"><span class="kw">class</span> Dog:</a>
|
|
|
<a class="sourceLine" id="cb13-2" data-line-number="2"></a>
|
|
|
<a class="sourceLine" id="cb13-3" data-line-number="3"> <span class="co">### Here, we define class variables. </span><span class="al">###</span></a>
|
|
|
<a class="sourceLine" id="cb13-4" data-line-number="4"> <span class="co"># These are the same for ALL dogs.</span></a>
|
|
|
<a class="sourceLine" id="cb13-5" data-line-number="5"> total_dogs <span class="op">=</span> <span class="dv">0</span></a>
|
|
|
<a class="sourceLine" id="cb13-6" data-line-number="6"></a>
|
|
|
<a class="sourceLine" id="cb13-7" data-line-number="7"> <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>, name<span class="op">=</span><span class="st">""</span>, age<span class="op">=</span><span class="dv">0</span>):</a>
|
|
|
<a class="sourceLine" id="cb13-8" data-line-number="8"></a>
|
|
|
<a class="sourceLine" id="cb13-9" data-line-number="9"> <span class="co">### These are instance variables. </span><span class="al">###</span></a>
|
|
|
<a class="sourceLine" id="cb13-10" data-line-number="10"> <span class="va">self</span>.name <span class="op">=</span> name</a>
|
|
|
<a class="sourceLine" id="cb13-11" data-line-number="11"> <span class="va">self</span>.age <span class="op">=</span> age</a>
|
|
|
<a class="sourceLine" id="cb13-12" data-line-number="12"> <span class="bu">print</span>(name, <span class="st">"created."</span>)</a>
|
|
|
<a class="sourceLine" id="cb13-13" data-line-number="13"></a>
|
|
|
<a class="sourceLine" id="cb13-14" data-line-number="14"> <span class="kw">def</span> bark_hello(<span class="va">self</span>):</a>
|
|
|
<a class="sourceLine" id="cb13-15" data-line-number="15"> <span class="bu">print</span>(<span class="st">"Woof! I am called"</span>, <span class="va">self</span>.name, <span class="st">"; I am"</span>, <span class="va">self</span>.age, <span class="st">"human-years old"</span>)</a>
|
|
|
<a class="sourceLine" id="cb13-16" data-line-number="16"> <span class="bu">print</span>(<span class="st">"There are"</span>, Dog.total_dogs, <span class="st">"dogs in this room!"</span>) <span class="co"># There's no "self" — we call the Dog class name!</span></a>
|
|
|
<a class="sourceLine" id="cb13-17" data-line-number="17"></a>
|
|
|
<a class="sourceLine" id="cb13-18" data-line-number="18">molly <span class="op">=</span> Dog(<span class="st">"Molly"</span>, <span class="dv">8</span>)</a>
|
|
|
<a class="sourceLine" id="cb13-19" data-line-number="19">molly.bark_hello()</a>
|
|
|
<a class="sourceLine" id="cb13-20" data-line-number="20"></a>
|
|
|
<a class="sourceLine" id="cb13-21" data-line-number="21">sheera <span class="op">=</span> Dog(<span class="st">"Sheera"</span>, <span class="dv">5</span>)</a>
|
|
|
<a class="sourceLine" id="cb13-22" data-line-number="22">sheera.bark_hello()</a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>Demo this, but don’t worry about having students follow along. Their understanding is more important than their typing.</li>
|
|
|
</ul>
|
|
|
<p><strong>Talking Points:</strong></p>
|
|
|
<ul>
|
|
|
<li>Our <code>Dog</code> class had variables attached to <code>self</code> that exist independently for each object that’s created.
|
|
|
<ul>
|
|
|
<li>These are called <strong>instance variables</strong>.</li>
|
|
|
<li>Each object instance has its own copies of these variables, and they can vary across objects.</li>
|
|
|
</ul></li>
|
|
|
<li>We can attach variables to the class itself so that there’s one single thing that exists for an entire class.
|
|
|
<ul>
|
|
|
<li>These are called <strong>class variables</strong>.</li>
|
|
|
</ul></li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="i-do-tallying-dogs" class="level2">
|
|
|
<h2>I Do: Tallying Dogs</h2>
|
|
|
<p>We can increment the <code>class</code> variable any time.</p>
|
|
|
<div class="sourceCode" id="cb14"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb14-1" data-line-number="1"><span class="kw">class</span> Dog:</a>
|
|
|
<a class="sourceLine" id="cb14-2" data-line-number="2"> total_dogs <span class="op">=</span> <span class="dv">0</span></a>
|
|
|
<a class="sourceLine" id="cb14-3" data-line-number="3"> <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>, name<span class="op">=</span><span class="st">""</span>, age<span class="op">=</span><span class="dv">0</span>):</a>
|
|
|
<a class="sourceLine" id="cb14-4" data-line-number="4"> <span class="va">self</span>.name <span class="op">=</span> name</a>
|
|
|
<a class="sourceLine" id="cb14-5" data-line-number="5"> <span class="va">self</span>.age <span class="op">=</span> age</a>
|
|
|
<a class="sourceLine" id="cb14-6" data-line-number="6"> Dog.total_dogs <span class="op">+=</span> <span class="dv">1</span> <span class="co"># We can increment this here!</span></a>
|
|
|
<a class="sourceLine" id="cb14-7" data-line-number="7"> <span class="bu">print</span>(name, <span class="st">"created:"</span>)</a>
|
|
|
<a class="sourceLine" id="cb14-8" data-line-number="8"></a>
|
|
|
<a class="sourceLine" id="cb14-9" data-line-number="9"> <span class="kw">def</span> bark_hello(<span class="va">self</span>):</a>
|
|
|
<a class="sourceLine" id="cb14-10" data-line-number="10"> <span class="bu">print</span>(<span class="st">"Woof! I am called"</span>, <span class="va">self</span>.name, <span class="st">"; I am"</span>, <span class="va">self</span>.age, <span class="st">"human-years old."</span>)</a>
|
|
|
<a class="sourceLine" id="cb14-11" data-line-number="11"> <span class="bu">print</span>(<span class="st">"There are"</span>, Dog.total_dogs, <span class="st">"dogs in this room!"</span>)</a>
|
|
|
<a class="sourceLine" id="cb14-12" data-line-number="12"></a>
|
|
|
<a class="sourceLine" id="cb14-13" data-line-number="13">molly <span class="op">=</span> Dog(<span class="st">"Molly"</span>, <span class="dv">8</span>)</a>
|
|
|
<a class="sourceLine" id="cb14-14" data-line-number="14">molly.bark_hello()</a>
|
|
|
<a class="sourceLine" id="cb14-15" data-line-number="15"></a>
|
|
|
<a class="sourceLine" id="cb14-16" data-line-number="16">sheera <span class="op">=</span> Dog(<span class="st">"Sheera"</span>, <span class="dv">5</span>)</a>
|
|
|
<a class="sourceLine" id="cb14-17" data-line-number="17">sheera.bark_hello()</a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>Again, note that <code>self</code> doesn’t exist for the <code>class</code> variables! Really stress when <code>self</code> is there and when it isn’t.</li>
|
|
|
<li>Show it increasing!</li>
|
|
|
<li>Once students get the hang of this, create a method that increments <code>total_dogs</code> as well.</li>
|
|
|
</ul>
|
|
|
<p><strong>Talking Points:</strong></p>
|
|
|
<ul>
|
|
|
<li>We can keep a tally of how many <code>dog</code>s we have running around in our app.</li>
|
|
|
<li>We could put a copy of the tally in each <code>dog</code> object, but that’s not efficient, as we would be duplicating a value in memory multiple times, and we would have to update the value in every <code>dog</code> object in order to keep it accurate.</li>
|
|
|
<li>It’s much better if we can store it in the class. That way, each <code>dog</code> object can access it, but we only need to store it and set it in one place.</li>
|
|
|
<li>Finally, we create a new <code>dog</code>. The <code>__init__</code> method increments the <code>total_dogs</code> counter, which is stored in the <code>Dog</code> class itself. We can access the value stored in <code>Dog.total_dogs</code> inside our script, and each <code>dog</code> object can access it from their own functions.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="partner-exercise-create-a-music-genre-class" class="level2">
|
|
|
<h2>Partner Exercise: Create a Music Genre Class</h2>
|
|
|
<p>Pair up! Create a new file, <code>Band.py</code>.</p>
|
|
|
<ul>
|
|
|
<li>Define a class, <code>Band</code>, with these instance variables: <code>"genre"</code>, <code>"band_name"</code>, and <code>"albums_released"</code> (defaulting to <code>0</code>).</li>
|
|
|
<li>Give <code>Band</code> a method called <code>print_stats()</code>, which prints a string like <code>"The rock band Queen has 15 albums."</code></li>
|
|
|
<li>Create a class variable, <code>number_of_bands</code>, that tracks the number of bands created.</li>
|
|
|
</ul>
|
|
|
<p>Test your code with calls like:</p>
|
|
|
<div class="sourceCode" id="cb15"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb15-1" data-line-number="1">my_band <span class="op">=</span> (<span class="st">"Queen"</span>, <span class="dv">15</span>, <span class="st">"rock"</span>)</a></code></pre></div>
|
|
|
<p><strong>Bonus:</strong> If the genre provided isn’t <code>"pop"</code>, <code>"classical"</code>, or <code>"rock"</code>, print out <code>"This isn't a genre I know."</code></p>
|
|
|
<aside class="notes">
|
|
|
<p>5-10 MINUTES</p>
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>After students do this, create it and talk through it.</li>
|
|
|
<li>If you’re running short on time, skip an exercise and assign it as homework (the next slide’s exercise is more difficult).</li>
|
|
|
</ul>
|
|
|
<p><strong>Talking Points:</strong></p>
|
|
|
<ul>
|
|
|
<li>Imagine that you are working with music data of all different types of genres and want to ultimately define three different classes of music (pop, classical, and rock).</li>
|
|
|
<li>Things to think about:
|
|
|
<ul>
|
|
|
<li>Starting values for variables should be set in the <code>__init__</code> method.</li>
|
|
|
<li>Class variables are declared inside the class but outside any methods.</li>
|
|
|
<li>Instance variables are declared inside the <code>__init__</code> method.</li>
|
|
|
<li>Does your <code>__init__</code> method need to accept any parameters?</li>
|
|
|
</ul></li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="partner-exercise-create-a-bankaccount-class" class="level2">
|
|
|
<h2>Partner Exercise: Create a <code>BankAccount</code> Class</h2>
|
|
|
<p>Switch drivers! Create a new class (and file), <code>Bank.py</code>.</p>
|
|
|
<p>Bank accounts should:</p>
|
|
|
<ul>
|
|
|
<li>Be created with the <code>accountType</code> property (either <code>"savings"</code> or <code>"checking"</code>).</li>
|
|
|
<li>Keep track of its current <code>balance</code>, which always starts at <code>0</code>.</li>
|
|
|
<li>Have access to <code>deposit()</code> and <code>withdraw()</code> methods, which take in an integer and update <code>balance</code> accordingly.</li>
|
|
|
<li>Have a class-level variable tracking the total amount of money in all accounts, adding or subtracting whenever <code>balance</code> changes.</li>
|
|
|
</ul>
|
|
|
<p><strong>Bonus:</strong> Start each account with an additional <code>overdraftFees</code> property that begins at <code>0</code>. If a call to <code>withdraw()</code> ends with the <code>balance</code> below <code>0</code>, then <code>overdraftFees</code> should be incremented by <code>20</code>.</p>
|
|
|
<aside class="notes">
|
|
|
<p>10 MINUTES</p>
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>This is tough! Walk around the room and check for questions and understanding.</li>
|
|
|
<li>After students do this, create it and talk through it.</li>
|
|
|
<li>If you’re running short on time, skip an exercise and assign it as homework.</li>
|
|
|
</ul>
|
|
|
<p><strong>Talking Points:</strong></p>
|
|
|
<ul>
|
|
|
<li>(The same as above.)</li>
|
|
|
<li>Things to think about:
|
|
|
<ul>
|
|
|
<li>Starting values for variables should be set in the <code>__init__</code> method.</li>
|
|
|
<li>Class variables are declared inside the class but outside any methods.</li>
|
|
|
<li>Instance variables are declared inside the <code>__init__</code> method.</li>
|
|
|
<li>Does your <code>__init__</code> method need to accept any parameters?</li>
|
|
|
</ul></li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="knowledge-check-select-the-best-answer" class="level2">
|
|
|
<h2>Knowledge Check: Select the Best Answer</h2>
|
|
|
<p>Consider the following class definition for <code>Cat</code>:</p>
|
|
|
<div class="sourceCode" id="cb16"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb16-1" data-line-number="1"><span class="kw">class</span> Cat:</a>
|
|
|
<a class="sourceLine" id="cb16-2" data-line-number="2"> <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>, name<span class="op">=</span><span class="st">'Lucky'</span>):</a>
|
|
|
<a class="sourceLine" id="cb16-3" data-line-number="3"> <span class="va">self</span>.name <span class="op">=</span> name</a>
|
|
|
<a class="sourceLine" id="cb16-4" data-line-number="4"> <span class="va">self</span>.fur <span class="op">=</span> short</a></code></pre></div>
|
|
|
<p>How would you instantiate a <code>Cat</code> object with the <code>name</code> attribute <code>'Furball'</code>?</p>
|
|
|
<ol type="1">
|
|
|
<li><code>mycat = Cat(name='Furball')</code></li>
|
|
|
<li><code>furball = Cat</code></li>
|
|
|
<li><code>mycat = Cat(self, name='Furball')</code></li>
|
|
|
<li><code>mycat = Cat.init(name='Furball')</code></li>
|
|
|
</ol>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>Have students guess before telling them.</li>
|
|
|
</ul>
|
|
|
<p><strong>Answer:</strong></p>
|
|
|
<ol type="1">
|
|
|
<li>The <code>__init__</code> function is automatically called when creating an object with the <code>Cat(name='Furball')</code> syntax.</li>
|
|
|
</ol>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="knowledge-check-select-all-that-apply." class="level2">
|
|
|
<h2>Knowledge Check: Select All That Apply.</h2>
|
|
|
<p>Which of the following statements are true about the <code>self</code> argument in class definitions?</p>
|
|
|
<ul>
|
|
|
<li>The user does not need to supply <code>self</code> when using instance methods.</li>
|
|
|
<li>The <code>self</code> argument is a reference to the instance object.</li>
|
|
|
<li>Any variable assigned with <code>self</code> (e.g., <code>self.var</code>) will be shared across instances of the class.</li>
|
|
|
<li>With an instance object, <code>obj</code>, entering <code>obj.self.var</code> will provide the value for <code>var</code> for that instance.</li>
|
|
|
</ul>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>Have students guess before telling them.</li>
|
|
|
</ul>
|
|
|
<p><strong>Answers:</strong></p>
|
|
|
<ol type="1">
|
|
|
<li>The user does not need to supply <code>self</code> when using instance methods.</li>
|
|
|
<li>The <code>self</code> argument is a reference to the instance object.</li>
|
|
|
</ol>
|
|
|
<p>Correct response explanation:</p>
|
|
|
<ul>
|
|
|
<li><code>self</code> is automatically passed into an instance method when you call it. <code>self</code> refers to the class and NOT the instance. <code>self.var</code> will not be shared between instances, as <code>self</code> refers to the class. Instances have no explicit <code>self</code> attribute.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="knowledge-check-select-the-best-answer-1" class="level2">
|
|
|
<h2>Knowledge Check: Select the Best Answer</h2>
|
|
|
<p>Consider the following code:</p>
|
|
|
<div class="sourceCode" id="cb17"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb17-1" data-line-number="1"><span class="kw">class</span> Shape(<span class="bu">object</span>):</a>
|
|
|
<a class="sourceLine" id="cb17-2" data-line-number="2"> possible <span class="op">=</span> [<span class="st">'triangle'</span>,<span class="st">'square'</span>,<span class="st">'circle'</span>,<span class="st">'pentagon'</span>,<span class="st">'polygon'</span>,<span class="st">'rectangle'</span>]</a>
|
|
|
<a class="sourceLine" id="cb17-3" data-line-number="3"></a>
|
|
|
<a class="sourceLine" id="cb17-4" data-line-number="4"> <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>, label<span class="op">=</span><span class="st">'triangle'</span>):</a>
|
|
|
<a class="sourceLine" id="cb17-5" data-line-number="5"> <span class="va">self</span>.label <span class="op">=</span> label</a>
|
|
|
<a class="sourceLine" id="cb17-6" data-line-number="6"></a>
|
|
|
<a class="sourceLine" id="cb17-7" data-line-number="7"> <span class="kw">def</span> is_possible(<span class="va">self</span>):</a>
|
|
|
<a class="sourceLine" id="cb17-8" data-line-number="8"> <span class="cf">if</span> <span class="va">self</span>.label <span class="kw">in</span> <span class="va">self</span>.possible:</a>
|
|
|
<a class="sourceLine" id="cb17-9" data-line-number="9"> <span class="bu">print</span>(<span class="st">'This is possible'</span>)</a>
|
|
|
<a class="sourceLine" id="cb17-10" data-line-number="10"> <span class="cf">else</span>:</a>
|
|
|
<a class="sourceLine" id="cb17-11" data-line-number="11"> <span class="bu">print</span>(<span class="st">'This is impossible'</span>)</a>
|
|
|
<a class="sourceLine" id="cb17-12" data-line-number="12"></a>
|
|
|
<a class="sourceLine" id="cb17-13" data-line-number="13">square <span class="op">=</span> Shape(label<span class="op">=</span><span class="st">'square'</span>)</a>
|
|
|
<a class="sourceLine" id="cb17-14" data-line-number="14">wormhole <span class="op">=</span> Shape(label<span class="op">=</span><span class="st">'wormhole'</span>)</a>
|
|
|
<a class="sourceLine" id="cb17-15" data-line-number="15">square.possible.append(<span class="st">'wormhole'</span>)</a></code></pre></div>
|
|
|
<p>If you were to enter <code>wormhole.is_possible()</code>, would the outcome be <code>"This is possible"</code> or <code>"This is impossible"</code>?</p>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>Have students guess before telling them.</li>
|
|
|
</ul>
|
|
|
<p><strong>Answer:</strong> <code>This is possible</code></p>
|
|
|
<p>Correct answer explanation:</p>
|
|
|
<ul>
|
|
|
<li>The possible list is defined at the class level as opposed to as an instance variable. When we append the string <code>'wormhole'</code> to the possible list of the <code>square</code> object, this list is shared with the wormhole instance. Therefore the output will be <code>This is possible</code>.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="summary-discussion" class="level2">
|
|
|
<h2>Summary: Discussion</h2>
|
|
|
<p>Let’s chat! Can anyone explain:</p>
|
|
|
<ul>
|
|
|
<li>What a class is?</li>
|
|
|
<li>What <code>__init__</code> does?</li>
|
|
|
<li>What an object is?</li>
|
|
|
<li>The point of <code>self</code>?</li>
|
|
|
<li>The two types of variables?</li>
|
|
|
</ul>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>Go through each question and make sure students can explain the answers. Check for understanding!</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="summary-and-qa" class="level2">
|
|
|
<h2>Summary and Q&A</h2>
|
|
|
<p>Class:</p>
|
|
|
<ul>
|
|
|
<li>A pre-defined structure that contains attributes and behaviors grouped together.</li>
|
|
|
<li>The blueprint.</li>
|
|
|
<li>Defined via a method call.</li>
|
|
|
<li>Contains an <code>__init__</code> method that takes in parameters to be assigned to an object.</li>
|
|
|
<li>E.g., the <code>Dog</code> class; the <code>List</code> class.</li>
|
|
|
</ul>
|
|
|
<p>Object:</p>
|
|
|
<ul>
|
|
|
<li>An instance of a class structure.<br />
|
|
|
</li>
|
|
|
<li>The items built from the blueprint.</li>
|
|
|
<li>E.g., the <code>gracie</code> object; the <code>my_list</code> object.</li>
|
|
|
</ul>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="summary-types-of-variables-in-a-class" class="level2">
|
|
|
<h2>Summary: Types of Variables in a Class</h2>
|
|
|
<p>Instance variables:</p>
|
|
|
<ul>
|
|
|
<li>Contain data types declared in the class but defined in each object.</li>
|
|
|
<li>Each <code>dog</code> has its own name and age.</li>
|
|
|
<li>Each <code>my_list</code> has its own elements.</li>
|
|
|
</ul>
|
|
|
<p>Class variables:</p>
|
|
|
<ul>
|
|
|
<li>Contain data and actions that span across all objects.</li>
|
|
|
<li>How many <code>dog</code> objects are there in total?</li>
|
|
|
<li>The <code>self</code> keyword lets us distinguish between variables that exist at the class level versus in each object.</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>
|