1+ """This module contains a code example related to
2+
3+ Think Python, 2nd Edition
4+ by Allen Downey
5+ http://thinkpython2.com
6+
7+ Copyright 2015 Allen Downey
8+
9+ License: http://creativecommons.org/licenses/by/4.0/
110"""
211
3- This program is part of an exercise in
4- Think Python: An Introduction to Software Design
5- Allen B. Downey
12+ from __future__ import print_function , division
13+
14+ """
615
7- This program explains and corrects a bug in BadKangaroo.py.
8- Before reading this, you should try to debug BadKangaroo.
16+ WARNING: this program contains a NASTY bug. I put
17+ it there on purpose as a debugging exercise, but
18+ you DO NOT want to emulate this example!
919
1020"""
1121
1222class Kangaroo (object ):
13- """a Kangaroo is a marsupial"""
23+ """A Kangaroo is a marsupial. """
1424
15- def __init__ (self , contents = []):
25+ def __init__ (self , name , contents = []):
26+ """Initialize the pouch contents.
27+
28+ name: string
29+ contents: initial pouch contents.
30+ """
1631 # The problem is the default value for contents.
1732 # Default values get evaluated ONCE, when the function
1833 # is defined; they don't get evaluated again when the
@@ -23,48 +38,60 @@ def __init__(self, contents=[]):
2338 # an empty list.
2439
2540 # After that, every Kangaroo that gets the default
26- # value get a reference to THE SAME list. If any
41+ # value gets a reference to THE SAME list. If any
2742 # Kangaroo modifies this shared list, they all see
2843 # the change.
2944
3045 # The next version of __init__ shows an idiomatic way
3146 # to avoid this problem.
47+ self .name = name
3248 self .pouch_contents = contents
3349
34- def __init__ (self , contents = None ):
50+ def __init__ (self , name , contents = None ):
51+ """Initialize the pouch contents.
52+
53+ name: string
54+ contents: initial pouch contents.
55+ """
3556 # In this version, the default value is None. When
3657 # __init__ runs, it checks the value of contents and,
3758 # if necessary, creates a new empty list. That way,
38- # every Kangaroo that gets the default value get a
59+ # every Kangaroo that gets the default value gets a
3960 # reference to a different list.
4061
4162 # As a general rule, you should avoid using a mutable
4263 # object as a default value, unless you really know
4364 # what you are doing.
65+ self .name = name
4466 if contents == None :
4567 contents = []
4668 self .pouch_contents = contents
4769
4870 def __str__ (self ):
49- """return a string representation of this Kangaroo and
50- the contents of the pouch, with one item per line """
51- t = [ object . __str__ ( self ) + ' with pouch contents:' ]
71+ """Return a string representaion of this Kangaroo.
72+ """
73+ t = [ self . name + ' has pouch contents:' ]
5274 for obj in self .pouch_contents :
5375 s = ' ' + object .__str__ (obj )
5476 t .append (s )
5577 return '\n ' .join (t )
5678
5779 def put_in_pouch (self , item ):
58- """add a new item to the pouch contents"""
80+ """Adds a new item to the pouch contents.
81+
82+ item: object to be added
83+ """
5984 self .pouch_contents .append (item )
6085
61- kanga = Kangaroo ()
62- roo = Kangaroo ()
86+
87+ kanga = Kangaroo ('Kanga' )
88+ roo = Kangaroo ('Roo' )
6389kanga .put_in_pouch ('wallet' )
6490kanga .put_in_pouch ('car keys' )
6591kanga .put_in_pouch (roo )
6692
67- print kanga
68- print ''
93+ print ( kanga )
94+ print ( roo )
6995
70- print roo
96+ # If you run this program as is, it seems to work.
97+ # To see the problem, trying printing roo.
0 commit comments