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

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

This produces:

[1]
[1,1]

but it should be just

[1]
[1]

lists are mutable, but we call function foo with no args in it. So it should always start with the default empty list var. Why does it 'recognize' there is var list and it's changed?

ERJAN
  • 23,696
  • 23
  • 72
  • 146

0 Answers0