Question 10: Python Default Parameters

This Python quiz question is about default parameter values and unexpected behavior when mutable objects like lists are used as default values.

What will be the output of the following code:

def foo(x=[]):
    x.append(1)
    return x

print(foo())
print(foo())

Select an option:🤔🧐















A: is the correct answer.

The key point here is that default parameter values in Python are evaluated only once when the function is defined, not each time the function is called.

So, the default list x is created once when the function is defined and is then shared across all calls to the function. This is why modifications to x persist between function calls.

Leave a Reply