What is local scope?

Questions & AnswersCategory: PythonWhat is local scope?
Geek Boy Staff asked 2 years ago

What is local scope?
a. From the moment we start the program in Python, we are in the local scope. Python turns the main script into a module called __main__ that stores the execution of the main program. The namespace of this module is the local scope of the program.
b. Local scope is created when the function is called. Each time we call the function we create a new local scope. By default, variables inside functions only exist in the local scope associated with the function call. When the function exits, the local scope is destroyed.
c. Global scope only appears when you nest functions within other functions.

1 Answers
Lokesh Kumar Staff answered 2 years ago

b. Local scope is created when the function is called. Each time we call the function we create a new local scope. By default, variables inside functions only exist in the local scope associated with the function call. When the function exits, the local scope is destroyed.