+ 1
[Python:OutputExplanation] Someone explain me output of this code. def func(a, L=[]): L.append(a) return(L)
I am confused with the output of this code def f(a,L=[]): L.append(a) return(L) print(f(3), f(4)) I am confused how is appended value to List L is memorized even after function exits? https://code.sololearn.com/czlOon5UjyaJ/#py
2 Answers
+ 1
I asked a similar question a while back
https://www.sololearn.com/Discuss/1017883/?ref=app
The best answer was from John Wells
https://docs.quantifiedcode.com/JUMP_LINK__&&__python__&&__JUMP_LINK-anti-patterns/correctness/mutable_default_value_as_argument.html
+ 3
i confuse too with your question.
first that create a function with a and L (list) default argument
than, a is inserted to L and finally return the L list.