What is the output of the following Python code snippet?
def f(x, lst=[]):
lst.append(x)
return lst
f(1)
print(f(2))
lst.append(x)
return lst
f(1)
print(f(2))
Correct answer
C. [1, 2]
9 views
C. [1, 2]
9 views
The default list is created once at definition time, so both calls append to the same list.
Asked in: Python Interview Functions
https://docs.python.org/3/tutorial/controlflow.html#defining-functionsNot sure why the answer is C? Ask here and get an explanation.