-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnested_func.py
More file actions
25 lines (18 loc) · 825 Bytes
/
nested_func.py
File metadata and controls
25 lines (18 loc) · 825 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Nested functions in Python
# A function that is defined inside another function is known as a nested function.
# Nested functions are able to access variables of the enclosing scope.
# In Python, these non-local variables can be accessed only within their scope and not outside their scope.
# This can be illustrated by the following example:
# Python program to illustrate
# nested functions
# global scope
def outerFunction(text):
# enclosed scope
def innerFunction():
# local scope
# non local variable
print(text)
innerFunction() # call the inner function
outerFunction('Hey!')
# As we can see innerFunction() can easily be accessed inside the outerFunction body but not outside of its body.
# Hence, here, innerFunction() is treated as a nested Function that uses text as a non-local variable.