Code Explanation:
๐น 1. Creating Function outer
def outer():
✅ Explanation:
A function named outer is defined.
Nothing executes yet.
Python only stores the function definition.
Current state:
outer → Function Object
๐น 2. Creating Local Variable
msg = "Python"
✅ Explanation:
When outer() runs, a local variable is created.
Value:
msg = "Python"
Memory:
outer()
└── msg = Python
๐น 3. Creating Nested Function
def inner():
✅ Explanation:
A function named inner is defined inside outer.
This function can access variables of outer.
Current structure:
outer
├── msg
└── inner
๐น 4. Return Statement Inside inner
return msg
✅ Explanation:
When inner() executes:
Python searches for:
msg
It is not inside inner.
So Python checks the enclosing function (outer).
Finds:
msg = "Python"
Returns:
"Python"
๐น 5. Calling inner()
return inner()
✅ Explanation:
Notice:
inner()
has parentheses.
So Python immediately executes inner.
Execution flow:
outer()
↓
inner()
↓
return msg
↓
"Python"
๐น 6. Returning Result From outer
inner() returns:
"Python"
Then:
return inner()
becomes:
return "Python"
So:
outer()
returns:
"Python"
๐น 7. Calling outer
print(outer())
✅ Explanation:
Python executes:
outer()
Inside outer:
msg = Python
↓
inner() called
↓
returns Python
↓
outer returns Python
๐น 8. Printing Result
print(outer())
prints:
Python
๐ฏ Final Output
Python

0 Comments:
Post a Comment