1

I need to understand why the calls to this function amends to the array which got a default initialization in the function declaration? Appreciate if any reference to the API specs

def f(i, values = []):
values.append(i)
print(values)
return values

Calls to:

  • f(1) prints -> [1]
  • f(2) prints -> [1, 2]
  • f(3) prints -> [1, 2, 3]

and so one

Waleed Z.
  • 31
  • 4
  • 3
    Does this answer your question? ["Least Astonishment" and the Mutable Default Argument](https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument) – MattDMo Aug 03 '21 at 12:25

0 Answers0