+ 1

Clarification on global and local scope of list.

def fuc(a,L=[]): L.append(a) return L print(fuc(1),fuc(3)) Ans : [1,3],[1,3] Both fuc(1) and fuc(3) return same result. WHY??

25th Apr 2021, 5:48 PM
Gbadegesin Muhammed
Gbadegesin Muhammed - avatar
2 ответов
+ 1
Your print statement processes both fuc() calls before invoking print. try printing fuc(1) and then printing fuc(3) on a different line.
25th Apr 2021, 6:16 PM
Jerry Hobby
Jerry Hobby - avatar
0
Most probably the comma in the print statement is indicating something like parallelism. Both fuc(1) and fuc(3) starts at the same time and founds a similar local scope like the one they were going to make. Then, thinking that local scope as their own they appends their input 'a' to the same list, L. At last python takes both the output from the same scope and just prints the same.
25th Apr 2021, 6:24 PM
Md. Faheem Hossain
Md. Faheem Hossain - avatar