413 - Introduction to Decorators - Global/Local scope

Screen Link:
Learn How To Use Decorators In Python. — Scopes Continued | Dataquest

I have a simple query - why is print (x) being called along with bar() ? Bar function already has a print (x) coded within it.

Or to put in another way - i expected to see 3 outputs, because of bar (), print(x) and foo().
There are only 2 outputs - 200 and 200.
Is there an interplay between print(x) and bar() because of which only one output is generated between the 2 of them - please let me know


Hi @raturi22:

This is probably a proof-of-concept by DQ to help you understand the concept of local, global and nonlocal scopes.

The nonlocal keyword means the subsequent variable to be defined is neither global or local. Based on the usual search order, the function will then search its parent function of the definition of the variable, if any. Here’s a link explaining this: Python Global, Local and Nonlocal variables (With Examples)

hmm… what gets printed onto the screen is invoked by print(). Calling the parent function (in this case foo()) doesn’t necessarily invoke the child function bar().

Hope this helps!


Because code executes sequentially top down, and in the body of foo(), print(x) comes after bar().

What made you think foo() should generate an output too?
In this code, there are 2 outputs because there are 2 print statements being executed.

Now i will address your 3 guesses of where 3 outputs come from. You didn’t say which print(x) you’re refering to, so i assume it’s the one after bar().

You’re right that bar() creates the 1st output 200. That’s because in the function body of bar(), there is a print(x) below x = 200.
You are right that print(x) creates the 2nd output 200. This print(x) is executed because foo() is called on last line.
If you say foo() produces the 3rd output, you are double counting the 2nd output.
foo()'s job is only to call the print(x) after bar(), like how bar()'s job is to call the print(x) after x = 200, which is where the 1st output came from.

Basically how many output u get is how many print statements there are in the code.
In this code there are 2 prints. Unless there is a looping structure, 2 prints cause 2 outputs.


Calling the parent function (in this case foo() ) doesn’t necessarily invoke the child function bar() .

Why is that so ? That’s where my confusion lies. I thought calling foo using ‘foo( )’ would call ‘bar ( )’ and that would print the x as per ‘print(x)’ , which is within the body of bar ( )

I agree that number of prints would define number of outputs. I just asked the same question to masterryan as well. Isn’t foo () calling and therefore causing the 3rd print () to execute ?

I thought calling foo using ‘foo( )’ would call ‘bar ( )’ and that would print the x as per ‘print(x)’ , which is within the body of bar ( ), below x =200

This website can help you visualize stacks and variable values and execution order. I wrote your example code in here so you can just click through and observe.

This channel gives comprehensive knowledge of how python works, helps clear up mental models: https://www.youtube.com/channel/UCjRVKSokBHlljocLdnUD9pg