Python Function Area

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() 
+5
source share
5 answers

My question is: why does fun0 modify val3 in the main function, and fun1 and fun2 do not change val4 and val7, respectively?

This is due to the way Python passes arguments.

When you pass arguments to a function, Python passes them by assignment. This means that it associates argument references with parameter names. This is the only way parameter and argument names are related. In the first example, you simply passed a link to val3 , and since both val3 and c referred to the same list object, the changes were reflected in both names.

However, when reassigning c within your functions, you replaced the link to lists c by holding and reassigning c new link.

+1
source

This is due to how lists and variables are stored. You can change the list inside a function because they are mutable objects. However, if you execute c = a[0] + b[0] , you create a local variable within fun1 and fun2 , which remains within the scope.

+2
source

Technically, when you pass an argument to a function, you pass a copy, not the original. Now there is a difference between passing a list and a passing variable. The list contains a reference to another object, while the variable contains a value. Therefore, when transferring a list, even if it is copied, it still refers to the same objects and can be changed inside the function, but when passing a variable, the copied object has nothing to do with the original. Hope this makes sense.

+1
source

That Cary Shindell is absolutely correct. Also, if you want to keep the updated values, here is how you can change your code to do this, I changed the print instructions since I tested this in python 3.6.1. Good question though.

 def main(): val1 = ['one'] val2 = ['two'] val3 = [''] fun0(val1, val2, val3) print(val3) val4 = [] val4 = fun1(val1, val2, val4) print(val4) val5 = 1 val6 = 1 val7 = 0 val7 = fun2(val5, val6, val7) print(val7) return 
0
source

as said earlier, lists are mutable. If you pass the list and change it, you will do it everywhere. This applies to all lists in fun0 .

in fun2 you do not pass any lists, and therefore c evaluated within fun2 , but not accessible from the outside.

I'm not sure about fun1 , but I think you are overwriting c with a new variable that is not accessible from the outside, as in fun2 .

0
source

Source: https://habr.com/ru/post/1269605/


All Articles