FAQ: Scope - Review

This community-built FAQ covers the “Review” 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 Review

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!

Hi,

How exactly does the nonlocal statement work?
Why does var2 exist in both the function2 local scope and in the function1 local scope? I would have expected var2 to only exist in function1’s local scope but not in function2’s local scope.
Furthermore, I noticed that the variable is both accessible and modifiable in both function1’s scope and in function2’s scope.

I made the following adjustments to the code to further investigate:

# Outer function
def function1():
  global var1
  var1 = 1
  var2 = 2
  # Inner function
  def function2():
    nonlocal var2
    global var3
    var2 += 1
    var3 = 3
    print(globals())
    print(locals())
  
  # Call inner function
  function2()
  print ('\n') #added line
  print(locals()) #added line
  var2 += 4 #added line
  print(locals()) #added line
# Call outer function
function1()
2 Likes

Hi!
I have the same question! So I am looking forward to hearing an answer from some expert.

I also tried to see how “var2” stored in memory. After using “nonlocal” its id changes and it starts to exist in both local and enclosing namespaces with the same id.

# Outer function
def function1():
  global var1
  var1 = 1
  var2 = 2
  # Inner function
  print("---ID of var2 in func1---")
  print(id(var2))
  def function2():
    nonlocal var2
    global var3
    var2 += 1
    var3 = 3
    print("---ID of var2 in func2---")
    print(id(var2))
    print(globals())
    print(locals())
  # Call inner function
  function2()
  print("---ID of var2 in func1 after func2 execution---")
  print(id(var2))
  print(locals())

# Call outer function
function1()

When printing locals() from this review, why is function2() not included?

I expected it to be included because it function2() is in the local namespace, inside the global namespace/outer function.

This is the code from the lesson; I added a “Globals” and “Locals” print text and “\n” so the output was easier to distinguish.

Thanks.

# Outer function
def function1():
  global var1
  var1 = 1
  var2 = 2
  # Inner function
  def function2():
    nonlocal var2
    global var3
    var2 += 1
    var3 = 3
    print("Globals",globals(),"\n")
    print("Locals",locals())
  
  # Call inner function
  function2()

# Call outer function
function1()