Skip to content

Instantly share code, notes, and snippets.

@vins31
Created July 11, 2014 21:04
Show Gist options
  • Save vins31/c5d47b1e81f57f0baa04 to your computer and use it in GitHub Desktop.
Save vins31/c5d47b1e81f57f0baa04 to your computer and use it in GitHub Desktop.
A common mistake. Don't fall into the trap!
"""
Educational code snippet for testing pointers familiarity.
Guess the output and find the bad practice.
"""
class Cat:
def __init__(self, s):
self.state = s
def observe(cats):
new_cats = []
for cat in cats:
print cat.state,
print "\n**"
for cat in cats:
print cat.state,
new_cats.append(Cat(cat.state))
cat.state = "dead"
new_cats = [new_cats[0], new_cats[0]]
return new_cats
cat1 = Cat("alive")
cats = [cat1, cat1]
cats = observe(cats)
print "\n--"
for cat in cats:
print cat.state,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment