Question #114

What is the output of the following code snippet?
def foo(a=[]):
    a.append(5)
    return a

foo()
foo()
foo()

The list keeps getting longer and longer. The function keeps using the same object a as a default argument. The first time the function is executed, a does not exist: it is created as an empty list. The second time the function is executed, a already exists: 5 is appended to it. (effbot.org/zone/default-values.htm)


Comment on Disqus:

Comment on Twitter:

Question difficulty: šŸ”µšŸ”µšŸ”µšŸ”µšŸ”µ


Similar questions: