0
votes

I am new to python. I run a code like this:

def funtest(L=[]):
    L.append(L)
    return L
print(funtest([2]))
print(funtest([3]))
print(funtest([4]))

real output is in below.

[2, [...]]
[3, [...]]
[4, [...]]

It is not a new problem about the Mutable Default Arguments. see before: Least Astonishment” and the Mutable Default Argument

According to the Mutable Default Arguments rule, I thought the output should like this:

[2, [...]]
[2,3, [...]]
[2,3,4, [...]]

Does anyone have idea about that? Thanks

1
because that question so I am asking this oneJiang Liang

1 Answers

2
votes

default arguments come in picture when you don't pass any arguments to the function,

here you are passing arguments to the function namely [2], [3] and [4] and hence default value is not being used

in your question you mention that for first print output should be [2, [...]] if you are able to understand that , then next ones are exactly same as default argument never comes in the picture.. just focus on how [2, [...]] came