I have this code written in Python:
def trick(l,i):
if i==1:
return l[0];
else :
return l[1]
def foo(x):
x[0] = 'def'
x[1] = 'abc'
return x[1]
q = ['abc', 'def']
print(trick(q,1) == foo(q))
print(trick(q,1) == foo(q))
print(trick(q,0) == foo(q))
The output is this:
True
False
True
Why does the second print statement print "False" even though the second and the first print statements are the same. And when I visualized this code in Python tutor I got to know that the foo(x) function actually changes/swaps the elements in the original/global list 'q' even though 'x' should have local scope.
I can't understand this. Please help.
xis mutable. Nothing to do with the scope. you changed the items inx