Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exercise 17.2: GoodKangaroo.py has its own pitfall #100

Open
jgmbenoit opened this issue Aug 10, 2022 · 0 comments
Open

exercise 17.2: GoodKangaroo.py has its own pitfall #100

jgmbenoit opened this issue Aug 10, 2022 · 0 comments

Comments

@jgmbenoit
Copy link

The suggested solution has a pitfall that somehow reproduces the same issue.
If we use the same content to initialize kanga and roo, then a new content put in one of them also appears in the other one.
More specifically, if we replace

kanga = Kangaroo('Kanga')
roo = Kangaroo('Roo')

by

prepouch=['red','green','blue']
kanga = Kangaroo('Kanga',precontents)
roo = Kangaroo('Roo',precontents)

then the output becomes

Kanga has pouch contents:
    'red'
    'green'
    'blue'
    'wallet'
    'car keys'
    <__main__.Kangaroo object at 0x7f2b4c049e20>
Roo has pouch contents:
    'red'
    'green'
    'blue'
    'wallet'
    'car keys'
    <__main__.Kangaroo object at 0x7f2b4c049e20>

while we may expect

Kanga has pouch contents:
    'red'
    'green'
    'blue'
    'wallet'
    'car keys'
    <__main__.Kangaroo object at 0x7f0abfdc4d30>
Roo has pouch contents:
    'red'
    'green'
    'blue'

I obtained the last output by assigning a copy of contents:

self.pouch_contents = contents[:]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant