0

why does this code

def f(sum, l=[]):
    l.append(sum)
    print(l)

f(10)
f(10)

return

[10]
[10, 10]

I cannon understand why shouldn't it return

[10]
[10]
prupru
  • 135
  • 6
  • You're setting l = [] to l = [10], so when you re-run the function it appends 10 to l = [10] thus giving you l = [10,10] – jasonmzx Feb 23 '21 at 14:34
  • As List is mutable object above behaviour is observed, similar behaviour is not observed in immutable objects chek https://pythonsimplified.com/mutability-immutability-in-python/ – Anvesh Feb 23 '21 at 14:40

0 Answers0