Created
July 11, 2014 21:04
-
-
Save vins31/c5d47b1e81f57f0baa04 to your computer and use it in GitHub Desktop.
A common mistake. Don't fall into the trap!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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