Scope of variables in a list comprehension
Say you're running the following code in Python.
words = ['lions', 'tigers', 'bears']
word = 'my'
bangs = [word + '!' for word in words]
print word
What do you expect this will print? Since I'm asking you've probably
guessed the answer is "bears" and not "my". I did not expect this
behavior, and it made me go slightly nuts over a bug.
No comments:
Post a Comment