What you will learn
- Define and identify: method,
__str__,__add__, operator overloading - Create a class with an
__init__method - Understand and use the
selfargument - Instantiate a class with an argument
Reading
Do Now
In your Console
Type the following
class Time(object):
def __init__(self, hour, minute, second):
self.hour = hour
self.minute = minute
self.second = second
time1 = Time(5, 32, 0)
time2 = Time(23, 11, 11)
print(time1)
print(time2)
In your Notebook
Respond to the following
- Based on what is output, how can you tell the difference between
time1andtime2? - What happens if you try to add
time1addtime2?
Lab
Instructions
- Finish writing the
__add__method for the time class from the Do Now. - Write a definition for a class named Kangaroo with the following methods:
- An
__init__method that initializes an attribute namedpouch_contentsto an empty list. - A method named
put_in_pouchthat takes an object of any type and adds it topouch_contents. - A
__str__method that returns a string representation of the Kangaroo object and the contents of the pouch.
- An
Tips to give students
- This exercise is a cautionary tale about one of the most common, and difficult to find, errors in Python
- TypeError: Can’t convert ‘list’ object to str implicitly
- Use the
str()function to convert the list object to a string. - Test your code by creating two Kangaroo objects
- assign them to variables named
kangaandroo - add
rooto the contents ofkanga’s pouch
- assign them to variables named
Extra Credit
Return to your Pet class from Lab 7.02. Research the isinstance function to write a method, is_friend that will take in another pet and return True if the two pets are friends, and false if they are not.
Rules
- If they are both dogs they are friends.
- If the instance is a dog and the other pet is a cat, they are friends.
- If the instance is a cat and the other is a dog they are not friends.
- If they are both cats they are not friends.
Check your understanding
Pick an answer for each question. You can try again until you get it right.
1. What does print(time1) show by default if the class has no __str__ method?
- The hour, minute, and second values
- An error message
- A generic description of the object, such as its class and memory location
- Nothing
Without __str__, Python cannot know how to describe your object nicely, so it prints a generic default.
2. What does defining __add__ in a class let you do?
- Add new attributes to every instance
- Use the
+operator on instances of that class - Make the class inherit from another
- Add methods after the class is created
This is operator overloading: a + b calls a.__add__(b).
3. How is a method different from an ordinary function?
- A method cannot take arguments
- A method always returns a string
- A method can only be called once
- A method is defined inside a class and is called on an instance, which is passed as
self
Methods belong to a class and automatically receive the instance they are called on.