Students will be able to...
- Define and identify: method,
__str__
,__add__
, operator overloading. - Create a class with an
__init__
method. - Understand and use the
self
argument. - Instantiate a class with an argument.
- 7.03 Slide Deck
- Do Now
- Lab - Kangaroo Class (docx) (pdf)
- Associated Reading
- Read through the do now, lesson, and lab so that you are familiar with the requirements and can assist students.
Duration | Description |
---|---|
5 Minutes | Do Now |
10 Minutes | Lesson |
35 Minutes | Lab |
5 Minutes | Debrief |
- Display the Do Now on the board.
- Students will find that when they try to print the two different Time objects, it produces output that's not particularly useful or readable.
- Students will also discover that adding objects doesn't work...yet!
- a function inside of a class.
- The first argument is always
self
.
- Ask students what method we have already seen and used previously. (
init
) - Ask students how they would distinguish between the two time variables.
- Need a method called
__str__
. - This will get called when you print an object.
- it returns a string that is easy to read and understand.
- Have the students practice writing
__str__
for theTime
class for 5 minutes. - Have a student write up their string method on the board.
- A method that gets called when the plus sign is used between two
Time
objects. - In this case it takes as parameters
self
and anotherTime
object and returns aTime
object that is the sum of both. - Overwriting add is called operator overloading because you are re-writing the code used to make the + work.
- Work together with students to come up with the add time algorithm.
- Have students finish up the time adding method.
- Have students work on kangaroo lab.
- Go over students' questions and demonstrate some students' successfully completed labs.
- Review what a method is, as well as what specific methods were used in today's lab.
Students that are moving quickly should work on the bonus assignment in the lab or assist a partner that is struggling.