def func(a,L=[]): L.append(a) return L print(func (3),func (1)) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

def func(a,L=[]): L.append(a) return L print(func (3),func (1))

Can someone please explain hoe this return [3,1] [3,1]?

9th Apr 2020, 11:49 AM
kedar padia
kedar padia - avatar
1 Answer
0
<L> refer to the same list object. First call adds 3 to the list then return it. Second call adds 1 to the list and return it also. But they are actually one list. Instead of printing the returned list object, you can verify this by printing the returned list ID. print(id(func(3)), id(func(1)), sep = '\n') You'll notice they are identical object : )
9th Apr 2020, 1:02 PM
Ipang