I have a question about scope in python functions. I included an example demonstrating the problem I am having.
fun0 overrides the first entry in the varible c list. This change is reflected outside fun0, even if I do not return any values ββfrom fun0.
fun1 completely overrides the variable c, but the change is not reflected outside fun1. Similarly, fun2 overrides c, and the change is not reflected outside fun2.
My question is: why does fun0 modify val3 in the main function, and fun1 and fun2 do not change val4 and val7, respectively?
def fun0(a, b, c): c[0] = a[0] + b[0] return def fun1(a, b, c): c = a[0] + b[0] return def fun2(a, b, c): c = a + b return def main(): val1 = ['one'] val2 = ['two'] val3 = [''] fun0(val1, val2, val3) print val3 val4 = [] fun1(val1, val2, val4) print val4 val5 = 1 val6 = 1 val7 = 0 fun2(val5, val6, val7) print val7 return if __name__=='__main__': main()
source share