Step-by-step Breakdown:
-
Function Definition:
-
This defines a function named status().
-
Inside the function, print(flag) is written, but flag is not yet defined inside the function — so Python will look for it outside the function (i.e., in the global scope) when the function runs.
-
-
Variable Assignment:
-
A global variable flag is assigned the value True.
-
-
Function Call:
-
Now the function status() is called.
-
Inside the function, print(flag) executes.
-
Python looks for flag:
-
Not found in local scope (inside status()).
-
Found in global scope: flag = True.
-
-
✅ Final Output:
In Short:
Even though flag is used inside a function, it's accessed from the global scope because it's not redefined inside the function.


0 Comments:
Post a Comment