Show what happens when a variable defined outside a function has the same name as a local variable inside a function. Explain what happens to the value of each variable as the program runs.
In fact - nothing will happen. Each variable will live it's own life in it's scope.
Example:
def my_func():
var = 20 # inner variable
return var
var = 10 # outer variable
print(var)
print(my_func())
print(var)
Output:
10
20
10
Any change of inner variable var will not affect outer variable var and vice versa.
Comments
Leave a comment