Classes and Objects
Define new types of data as a collection of functions and values.
Lesson: Classes and Objects
Time: 40+ mins
IntroductionIn this lesson, students will define and organize collections of functions and data as objects and classes.
- Class: A way of organizing functions and data in Python. Classes are used to create objects, like the Turtle object created with the Turtle class. Classes are defined using the class keyword followed by the name of the class and a colon. Here’s what the syntax looks like:
The name of the class should be capitalized. All the functions and variables included in the indented code below the class statement are included in the class.
- Constructor: A special function defined in the class (_init_) that is automatically called when the object is created. For example, to create an object for the Turtle class, you would use the instruction Turtle() and assign it to a variable name.
- Reference: The variable that stores an object is actually just storing the reference to a particular place in a computer's memory that contains the data for that object. It is referring to the object, not storing the actual object.
- Attribute: Data that is stored in the memory location of an object. Attributes can be used just like a variable and can be lists, dictionaries, integers, and more.
- Define classes with functions and variables
- Use classes to create objects
- Change the values in attributes of objects
- Create classes that take multiple parameters and set the values in the attributes when an object is created
- Computers (1 per student) with student account access to Tynker.com
Warm-Up (5 minutes)Ask students to answer the following short-response question:
- What are some benefits of organizing a collection of items?
Activities (35 minutes)Facilitate as students complete the Classes and Objects modules on their own:
1. Classes and Objects (Tutorial)
- Students will read a short document that explains classes and objects.
- This module does not contain puzzles.
- Tell students to click the “Next” button (located at the bottom of the document) to move on to the next module.
- In this module, students will read a short document that explains how to define a class.
- Optional: Ask students, “Would it be easy to reuse John’s program if he printed a longer introduction? Why or why not?”
- Are students struggling with the “Restaurant class” puzzle? Check that students are including a “createPizza()” and “deliverPizza()” functions that both print some output. Give a hint: Tell students to use code from the “Creating human objects” example as a reference.
- In this module, students will read a short document that explains attributes.
- Check that students are exploring the “Try This” section, which encourages students to remove the comment from the program below to see how the output changes when each attribute is set.
- Are students struggling with the “Vehicle class” puzzle? Give a hint: Tell students to use an “elif” statement and brainstorm possible solutions with a neighbor.
- In this module, students will read a short document that explains how to define a constructor.
- Emphasize to students that there are two underscores on either side of the “__init__()” function.
- Are students struggling with the “Building constructor” puzzle? Give a hint: Tell students to use the code in the example as a reference.
- In this module, students will read a short document that expands on parameter concepts learned in the previous lessons.
- Optional: Before students select the “play” button in the examples, ask them to write down what they think the code will output.
- Are students struggling with the “Car class” puzzle? Give a hint: Tell students they need to define the “Car” class--they do not need to call it.
- In this module, students will read a short document that explains how to define multiple classes in the same program.
- Optional: Encourage students to modify the “store inventory” code by adding more products. Their code might look like this:
c = Product("milk", 2.49)
- Are students struggling with the “Bank accounts” puzzle? Encourage them to break up the problem into smaller sections and brainstorm possible solutions with a neighbor. Give a hint: Tell students to use code from the “store inventory” example as a reference.
- Students will need to demonstrate their knowledge of concepts learned from this lesson by solving 2 different quiz problems. The first problem is a puzzle and the second problem is a DIY. Make sure students click the “submit project” button. Otherwise, you cannot see their work!
Discussion Questions/Follow-Up Activities (20 minutes)Lead a discussion with your students:
- Who can remember some examples of how programs from this lesson can be applied to real-world situations? (A program for a store inventory can help manage a store)
- How else can programs help with other real-world situations?
- What is something you would like to code?
- CCSS-ELA: SL.8.1, RI.9-10.3, RI.9-10.6, RI.11-12.3, RI.11-12.6, L.9-10.3, L.9-10.6, L.11-12.3, L.11-12.6
- CCSS-Math: HSN.Q.A.1, HSN.Q.A.2, HSN.Q.A.3, MP.1, MP.2, MP.4
- CSTA: 2-AP-11, 2-AP-12, 2-AP-13, 2-AP-14, 2-AP-15, 2-AP-17, 3A-AP-17, 3B-AP-11, 3B-AP-12
- CS CA: 6-8.AP.11, 6-8.AP.12, 6-8.AP.13, 6-8.AP.14, 6-8.AP.15, 6-8.AP.17, 9-12.AP.12, 9-12.AP.14, 9-12.AP.16
- ISTE: 1.c, 1.d, 4.d, 5.c, 5.d
Classes and ObjectsPython 201
- 7 Activities
- 1 Completion Badge