Im trying to figure out how this code prints 5 7 as output. Im unable to understand how this code actually executes.
Code:
def outer_function(x):
def inner_increment(x):
return x + 2
y = inner_increment(x)
print(x,y)
outer_function(5)
Output:
5 7
inner_incrementwas defined asdef inner_increment():and usingxfrom the scope ofouter_function. As it stands,inner_incrementis declaring it's own variablexwhich masks the parameter fromouter_function, using none of the scope fromouter_function. Because there is no dependency on the scope ofouter_function, there is no reason thatinner_incrementcould not just be defined as another top level function.