« Thousand dollar chicken
Main
Pudding Jacker »
Python Is Stupid In 3 Seconds
What do you think this Python program prints?
def foo(x=[]):
    x.append('hi')
    print x

foo()
foo()
foo()

Time's up! Did you think it prints this?
['hi']
['hi']
['hi']

If thats what you thought, then you're a reasonable person. But you're also incorrect. This is the actual output:
['hi']
['hi', 'hi']
['hi', 'hi', 'hi']

Because Python default argument initializers are evaluated only once, and are mutable. How's that for a gotcha? Geez.
Comments
blog comments powered by Disqus
The views expressed on this site are mine personally, and do not necessarily reflect the views of my employer.