Skip to content

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

Open
@jgmbenoit

Description

@jgmbenoit

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[:]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions