How does the output come as [1,5],[1,5] in Python?
Code: def func(a,L=): L.append(a) return(L) print(func(1),func(5)) Output ; [1,5][1,5] Shouldn't it be just ,[1,5]? What's happening?Is it because of parallelism?
The key word here is "memoization", which happens with the list declaration L=. The list is only initialized once with the first call, and then used each time the function is called again. with calling func(1), the number passed to the func will be appended to L. So when L is returned, it is . But not this value is returned but a reference to it. with calling func(5), the number passed to the func will be appended to the existing list. so when L is returned, it is [1,5]. After this second call of the function, the print() function will output the returned list L twice. As the reference to L has a value of [1,5], the result will be [1,5],[1,5].