1

so i have some function like this:

def func(ls=[]):
    for i in range(10):
        ls.append(i)
    print(ls)

in its arguments, the function stores some list that may be blank or if you set its value then it will become something else

so i expect that after calling it 2 times with blank arguments like this:

func()
func()

it will print

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

but instead it prints

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

i do understand how to fix it, but i don't understand why it happens.

4SadMemeXD
  • 101
  • 8

0 Answers0