+ 2
Why the output is not [1],[2],[3]
1 Answer
+ 3
unless declared the function f will assume the second argument to be 'values'.
initialization for any variable made in the function arguments will be only once. so, when you call the function the second time, it assumes the previous value of 'values'
in order to obtain your desired results, change your code to this
def f(i):
values = []
values.append(i)
print(values)
return values
f(1)
f(2)
f(3)
now, 'values' is a local variable and will be empty every time you call the function