- 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_contents
to an empty list. - A method named
put_in_pouch
that 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
- 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
kanga
androo
- add
roo
to the contents ofkanga
’s pouch
- assign them to variables named
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.
- 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.