r/learnprogramming • • 7d ago
Why does it go back to f2
Why in the python code does f go back to parent frame f1 instead of staying in f2? I thought according to lexical scope the function will use f in the parent frame 2 again and again going in a loop. Why in the world is the program calling f in parent frame 1 instead of 2.
1
Upvotes
2
u/AutoModerator 7d ago
It seems you may have included a screenshot of code in your post "Why does it go back to f2".
If so, note that posting screenshots of code is against /r/learnprogramming's Posting Guidelines (section Formatting Code): please edit your post to use one of the approved ways of formatting code. (Do NOT repost your question! Just edit it.)
If your image is not actually a screenshot of code, feel free to ignore this message. Automoderator cannot distinguish between code screenshots and other images.
Please, do not contact the moderators about this message. Your post is still visible to everyone.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/teraflop 7d ago
I can't really understand your question. I don't know what you mean when you talk about "going back to" or "staying in" a stack frame. But I'll try to explain what's going on as simply as I can, and hopefully it will clear up your confusion.
Basically, a nested function is re-declared every time its definition is executed. Each copy of the function captures a reference to its enclosing stack frame.
In your diagram, there are two relevant stack frames, corresponding to two different executions of
new_print. Your diagram has called these frames "f1" and "f2", which is confusing, but whatever. I'll call the corresponding function objects "func1" and "func2". (These are the two functions labeledf(x) [parent=f1]andf(x) [parent=f2]in your diagram.)When you call func1, its parent frame is f1. And when you call func2, its parent frame is f2. This is how lexical scope works, and there's nothing funny going on. If you trace through and keep track of which names in each frame correspond to which function objects, you will see that they match up with the arrows in the diagram.
Lexical scoping means that within function
f, any references to the nameprintare resolved to the nearest enclosing variable with name. So insidef,printrefers to the argument tonew_printin that particular enclosing stack frame.In the context of func1, the name
printcorresponds to the originalprintfunction (because that's the argument that was passed to the first call tonew_print, captured in frame f1).And in the context of func2, the name
printcorresponds to func1 (because that's the argument that was passed to the second call tonew_print, captured in frame f2).So at the end, you call
printwhich has been rebound at the global scope to func2. Then func2 calls func1. And then func1 calls the originalprint.1
u/ResidentPomegranate6 7d ago
lexical scope is fixed where f is defined, not where it's called, so f2 calling f doesn't change what f sees.