FAQ: Scope - Enclosing/Nonlocal Scope

This community-built FAQ covers the “Enclosing/Nonlocal Scope” exercise from the lesson “Scope”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Intermediate Python 3

FAQs on the exercise Enclosing/Nonlocal Scope

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

In the Enclosing/Nonlocal Scope page/lesson they give this code:

def outer_function(): enclosing_value = 'Enclosing Value' def nested_function(): nested_value = 'Nested Value' def second_nested(): print(enclosing_value) print(nested_value) second_nested() nested_function() outer_function()

which outputs as I expect.

When I delete the nested_fuction and outer_function call like this:

def outer_function(): enclosing_value = 'Enclosing Value' def nested_function(): nested_value = 'Nested Value' def second_nested(): print(enclosing_value) print(nested_value) second_nested()

there is no output. Why is this?

Hello @retawater
This is because you call the second_nested() within the nested_function() which is not called in your script at all. Even though, is is all enclosed in outer_function() so unless you call it, the underlying code wont print anything out.

Like a domino, 3rd one wont fall unless you touch 1st (beginning of the chain)

While trying to understand the enclosing scope, found that local namespace in nested function ‘sees’ only one of 2 variables out of enclosing function.
As shown below, the first print(locals()) shows just the square_feet variable while expected to see ‘paint_coverage’: 350 in the local namespace too.

def calc_paint_amount(width, height): square_feet = width * height # typically 1 galon cover 350 square feet paint_coverage = 350 # Write your code below! def calc_gallons(): print(locals()) #corrected coverage paint_coverage = 400 print(locals()) return square_feet / paint_coverage return calc_gallons() print('Number of paint gallons needed: ') print(str(calc_paint_amount(30,20)))

In the above code per purpose I used different values. The output I’m getting shows single variable while two are defined in the outer scope.

Number of paint gallons needed:
{‘square_feet’: 600}
{‘square_feet’: 600, ‘paint_coverage’: 400}
1.5

Any idea why nested function shows just one of the enclosing function variables?
Such a selective approach is odd for me.

Immutable objects, such as strings or numbers, can be accessed in nested functions, but cannot be modified.
Why?
Edit: I figured out why
It’s because when you create a new variable inside the nonlocal scope or the nested function, it will make a new local variable not overwriting the existing one!

1 Like