|
| 1 | +my_list = [1, 2, 3] |
| 2 | +my_var = 5 |
| 3 | +global var2 |
| 4 | +var2 = 8 |
| 5 | + |
| 6 | +non_global_var = 7 |
| 7 | + |
| 8 | + |
| 9 | +def add_to_list(fun_list): |
| 10 | + fun_list.append(4) # Making changes to the list by appending |
| 11 | + print 'In add_list function, fun_list =:', fun_list # [1, 2, 3, 4] |
| 12 | + print 'In add_list function, my_list =:', my_list # [1, 2, 3, 4] |
| 13 | + |
| 14 | + |
| 15 | +def change_var(var): |
| 16 | + print 'In change_var function, var =', var # 5 |
| 17 | + my_var = 10 |
| 18 | + print 'In change_var function, my_var =', my_var # 10 |
| 19 | + |
| 20 | + |
| 21 | +def change_var_globally(var): |
| 22 | + print 'In change_var_globally function, var =', var # 5 |
| 23 | + global my_var # global variable declaration in function works only |
| 24 | + my_var = 10 |
| 25 | + print 'In change_var_globally function, my_var =', my_var # 10 |
| 26 | + |
| 27 | + |
| 28 | +def check_global_var(): |
| 29 | + # I am not declaring any variable, still it is detecting the my_var because it was made set global |
| 30 | + print 'Checking global variable in another function:', my_var # 15 it works fine anywhere |
| 31 | + print 'Global variable declared outside of functions:', var2 # 8 |
| 32 | + # var2 = 9 I am not able to 'change' the global var declared outside of function becuase it will be local for this definition |
| 33 | + |
| 34 | + print 'non_global variable declared outside of the function:', non_global_var |
| 35 | + |
| 36 | + |
| 37 | +print 'Before add_to_list call, my_list =', my_list # [1, 2, 3] |
| 38 | +add_to_list(my_list) |
| 39 | +print 'After calling add_to_list, my_list =', my_list # [1, 2, 3, 4] |
| 40 | + |
| 41 | +print 'Before change_var call, my_var =', my_var # 5 |
| 42 | +change_var(my_var) |
| 43 | +print 'After calling change_var, my_var =', my_var # 5 |
| 44 | +# I get the same value though i change the variable value in function because it is not declared as global |
| 45 | + |
| 46 | +# now try to change the variable value by making it global in function definition |
| 47 | +change_var_globally(my_var) |
| 48 | +print 'After calling change_var_globally, my_var =', my_var # 10 |
| 49 | +# I get changed value for the variable after declaring that variable as global in defination |
| 50 | + |
| 51 | +my_var = 15 # changing the global variable value declared in defination |
| 52 | +check_global_var() |
| 53 | + |
| 54 | +''' |
| 55 | +Conclusion: |
| 56 | +1. global variable declared in function will have read as well as write access all over the program |
| 57 | +2. global variable declared outside the function definition will have read access all over the Programs but not able to write in any function |
| 58 | +3. global variable declared outside the function will be similar to normal variable |
| 59 | +4. normal variable can be used in the function (in fact it will work like global until any write operation. When you assign something to it, it becomes local) |
| 60 | +''' |
0 commit comments