This is saved in my file function_local.py
:
x = 50
def func(x):
print('x is', x)
x = 2
print('Changed local x to', x)
func(x)
print('x is still', x)
Output:
$ python function_local.py
x is 50
Changed local x to 2
x is still 50
Question: When we print the first line inside the Function, why does it print out 50, not 2? Even if it is said, in the function, that x = 2
?
this could be easily understand by this.
and the output
however if you want to access the
x
from outer scope after defining thex
infunc
there is a way, I am not sure if it would create a issue or not though.Following will be output of the run.