Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6797881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:39:48+00:00 2026-05-26T18:39:48+00:00

This question is a follow-up on a question about Python variable scope . Additional

  • 0

This question is a follow-up on a question about Python variable scope. Additional questions q1, q2 and answers can be found on SO, among even more.
The official Python documentation, and PEP 3104 are supposed to explain the details, but they don’t seem fully self-explanatory to me.

The topic I’m trying to solve is refactoring of code containing nonlocal/global by moving that code up/down one level of hierarchy.

What I do not understand are the implications of this sentence from the Python reference:

Names listed in a nonlocal statement, unlike to those listed in a
global statement, must refer to pre-existing bindings in an enclosing
scope (the scope in which a new binding should be created cannot be
determined unambiguously).

Given the following code on global scope:

var = 0
def outer():
    global var                  # line A
    var = 1
    def inner():
        nonlocal var            # line B
        var = 2

        print('inner:', var)

    inner()
    print('outer:', var)

outer()
print('main:', var)

Execution raises an error:

SyntaxError: no binding for nonlocal 'var' found

The code works (with different semantics, of course, if either line A is commented out:

inner: 2
outer: 2
main: 0

or line B is commented out:

inner: 2
outer: 1
main: 1

However, in the above example, and since nonlocal is supposed to bind var to the “enclosing scope”, I would have expected that line A binds the outer/var into global scope and line B then looks for outer/var and also rebinds inner/var to global/var. Instead it seems to not find it at all (due to the rebinding in line A, I suppose) and raise an error.

The desired result I expected was:

inner: 2
outer: 2
main: 2

Is this just one more example of the confusing state of mind of scoping in Python?

Or, to make this a constructive question:

  • How can such an example be written in a way that it does not matter at which level a function resides (having to exchange global with nonlocal and vice versa)?
  • If the functions reside at an intermediate, and unknown level of hierarchy, how could the author of outer() change the code that neither the outermost (in this case global) level, nor the inner() level have to be touched? –

In my humble understanding of the language, constructs like these (dependecies on closures) are just to be avoided. Others already have suggested to use other language features (classes, func attrs) to achieve this kind of context sensitivity.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:39:49+00:00Added an answer on May 26, 2026 at 6:39 pm

    global and nonlocal are not meant to be combined. They mean different things:

    • global means the name exists at the module level
    • nonlocal means the name exists in an outer lexical function scope

    The reason you are getting the original exception is because you told Python that var is nonlocal (meaning it is in an outer function definition), but there is no function-level binding for var in any outer function definition because you told Python in the outer function that var was global.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is a follow up on one of my other questions, Can I
This question is a follow up to my previous question about getting the HTML
Follow up to this question about GNU make : I've got a directory, flac
As kind of a follow up to this question about prefixes , I agree
This is a follow up question to a previous question I asked about calculating
This is a follow on from my previous question although this is about something
This is a follow-up to a previous question I had about interfaces. I received
This question is a follow up to: Why can’t I call a method outside
This question is a follow up and continuation of this question about a Privilege
This is a more specific question to follow up on [another question that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.