Lab 7.03 - Kangaroo Class
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.