Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

Views: 96169
License: OTHER
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/
10
"""
11
12
from __future__ import print_function, division
13
14
"""
15
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!
19
20
"""
21
22
class Kangaroo:
23
"""A Kangaroo is a marsupial."""
24
25
def __init__(self, name, contents=[]):
26
"""Initialize the pouch contents.
27
28
name: string
29
contents: initial pouch contents.
30
"""
31
self.name = name
32
self.pouch_contents = contents
33
34
def __str__(self):
35
"""Return a string representaion of this Kangaroo.
36
"""
37
t = [ self.name + ' has pouch contents:' ]
38
for obj in self.pouch_contents:
39
s = ' ' + object.__str__(obj)
40
t.append(s)
41
return '\n'.join(t)
42
43
def put_in_pouch(self, item):
44
"""Adds a new item to the pouch contents.
45
46
item: object to be added
47
"""
48
self.pouch_contents.append(item)
49
50
51
kanga = Kangaroo('Kanga')
52
roo = Kangaroo('Roo')
53
kanga.put_in_pouch('wallet')
54
kanga.put_in_pouch('car keys')
55
kanga.put_in_pouch(roo)
56
57
print(kanga)
58
59
# If you run this program as is, it seems to work.
60
# To see the problem, trying printing roo.
61
62
# Hint: to find the problem try running pylint.
63
64