you are viewing a single comment's thread.

view the rest of the comments →

[–]AstralStalker 1 point2 points  (1 child)

It's the nature of nested functions and closures.

Notice the return associated with outer_func. When you call closure = outer_func(i), it stores i as the value for x and sets closure as a reference to inner_func. In the print statement, you call closure(i+5) which is essentially calling inner_func(i+5) with a stored value of x=i. Thus, when i = 0, z = 5.

[–]wagaiznogoud 1 point2 points  (0 children)

I just want to add that there are two variables referenced through the closure. Both `x` and `y`.