+ 2

Question about functions

Can someone help in describing below functions how the output will be 18?! All results count as (3+6+9) = 18 ? OR!?? def f(x=[]): x+=[3] return sum(x) print(f()+f()+f()) The output is: 18

25th Jun 2018, 8:22 AM
Dolan
Dolan - avatar
3 ответов
+ 7
First call x becomes [3]. Second call x becomes [3, 3]. Third call x becomes [3, 3, 3]. Therefore, f returns 3, 6, and 9, which added together is your printed 18.
25th Jun 2018, 8:53 AM
John Wells
John Wells - avatar
+ 2
Thanks John
25th Jun 2018, 9:15 AM
Dolan
Dolan - avatar
+ 2
Right
19th Aug 2018, 1:53 PM
Priyanshu Soni
Priyanshu Soni - avatar