+ 1

why the answer is 5 and not 7 ???

x= 5 def funA(x,y): x=7 return x funA(x,4) print(x)

18th Jul 2016, 3:49 PM
Ahmed Kamal
Ahmed Kamal - avatar
4 Respostas
+ 2
varables named x in your first and your third line are different variables, you can have two with the same name because they are not in the same scope. so, when you assign 7 to x, you are actually assigning 7 to x from funA and not the global one. when funA is called, value of the global x is copied to the x which is the argument of the function. this code is equivalent to yours: x_global = 5 def funA(x_funA,y): x_funA=7 return x_funA funA(x_global,4) print(x_global)
18th Jul 2016, 6:59 PM
RedAnt
RedAnt - avatar
0
7
18th Jul 2016, 5:15 PM
Ranjith kumar
Ranjith kumar - avatar
0
@Ranjith kumar, No iam sure it is 5 i am already tried it and the answer was 5
18th Jul 2016, 5:53 PM
Ahmed Kamal
Ahmed Kamal - avatar
0
@RedAnt, thanks alot
18th Jul 2016, 7:13 PM
Ahmed Kamal
Ahmed Kamal - avatar