📚 The CoCalc Library - books, templates and other resources
License: OTHER
"""This module contains a code example related to12Think Python, 2nd Edition3by Allen Downey4http://thinkpython2.com56Copyright 2015 Allen Downey78License: http://creativecommons.org/licenses/by/4.0/9"""1011from __future__ import print_function, division1213"""1415WARNING: this program contains a NASTY bug. I put16it there on purpose as a debugging exercise, but17you DO NOT want to emulate this example!1819"""2021class Kangaroo:22"""A Kangaroo is a marsupial."""2324def __init__(self, name, contents=[]):25"""Initialize the pouch contents.2627name: string28contents: initial pouch contents.29"""30self.name = name31self.pouch_contents = contents3233def __str__(self):34"""Return a string representaion of this Kangaroo.35"""36t = [ self.name + ' has pouch contents:' ]37for obj in self.pouch_contents:38s = ' ' + object.__str__(obj)39t.append(s)40return '\n'.join(t)4142def put_in_pouch(self, item):43"""Adds a new item to the pouch contents.4445item: object to be added46"""47self.pouch_contents.append(item)484950kanga = Kangaroo('Kanga')51roo = Kangaroo('Roo')52kanga.put_in_pouch('wallet')53kanga.put_in_pouch('car keys')54kanga.put_in_pouch(roo)5556print(kanga)5758# If you run this program as is, it seems to work.59# To see the problem, trying printing roo.6061# Hint: to find the problem try running pylint.626364