Nowy kurs! Każdy programista powinien nauczyć się Generative AI!
Wypróbuj darmową lekcję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]?
1 Odpowiedź
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 : )