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

  • Home
  • SEARCH
  • 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 7676255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:09:31+00:00 2026-05-31T17:09:31+00:00

I have a little problem with local variables and python (2.7). I have a

  • 0

I have a little problem with local variables and python (2.7).

I have a little code :

def foo(a):
    def bar():
        print a
    return bar()

>>>foo(5)
5

Well, it’s working, but if want to modify a , like this :

def foo(a):
    def bar():
        a -= 1
    return bar()
>>>foo(5) 
UnboundLocalError: local variable 'a' referenced before assignment

So I must affect ‘a’ to another variable.

But I don’t understand this comportment.
Is it because when there is an assignment, python looks in the locals() variables and doesn’t find it ?

Thanks.

  • 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-31T17:09:32+00:00Added an answer on May 31, 2026 at 5:09 pm

    You’ve found something that used to be an issue in Python! The short answer is that you can’t do this in Python 2.x (though you can simulate) it, but you can in 3.x using the nonlocal keyword.

    See PEP 3104:

    Before version 2.1, Python’s treatment of scopes resembled that of
    standard C: within a file there were only two levels of scope, global
    and local. In C, this is a natural consequence of the fact that
    function definitions cannot be nested. But in Python, though functions
    are usually defined at the top level, a function definition can be
    executed anywhere. This gave Python the syntactic appearance of nested
    scoping without the semantics, and yielded inconsistencies that were
    surprising to some programmers — for example, a recursive function
    that worked at the top level would cease to work when moved inside
    another function, because the recursive function’s own name would no
    longer be visible in its body’s scope. This violates the intuition
    that a function should behave consistently when placed in different
    contexts. Here’s an example:

    def enclosing_function():
        def factorial(n):
            if n < 2:
                return 1
            return n * factorial(n - 1)  # fails with NameError
        print factorial(5)
    

    Python 2.1 moved closer to static nested scoping by making visible the
    names bound in all enclosing scopes (see PEP 227). This change makes
    the above code example work as expected. However, because any
    assignment to a name implicitly declares that name to be local, it is
    impossible to rebind a name in an outer scope (except when a global
    declaration forces the name to be global). Thus, the following code,
    intended to display a number that can be incremented and decremented
    by clicking buttons, doesn’t work as someone familiar with lexical
    scoping might expect:

    def make_scoreboard(frame, score=0):
        label = Label(frame)
        label.pack()
        for i in [-10, -1, 1, 10]:
            def increment(step=i):
                score = score + step  # fails with UnboundLocalError
                label['text'] = score
            button = Button(frame, text='%+d' % i, command=increment)
            button.pack()
        return label
    

    Python syntax doesn’t provide a way to indicate that the name score
    mentioned in increment refers to the variable score bound in
    make_scoreboard, not a local variable in increment. Users and
    developers of Python have expressed an interest in removing this
    limitation so that Python can have the full flexibility of the
    Algol-style scoping model that is now standard in many programming
    languages, including JavaScript, Perl, Ruby, Scheme, Smalltalk, C with
    GNU extensions, and C# 2.0.

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

Sidebar

Related Questions

I have a little problem. I had some JComboBox to a JDialog but they
I have a little app that runs fine on local dev appengine, but appengine
I have a little, but nasty problem: I have a database with many records,
I have a little problem. I want to create a query in my local
I have a little problem using Suds in Django (1.3, python 2.7). When I
i have little problem with boost::asio library. My app receive and process data asynchronously,
I have a little problem with a Listview. I can load it with listview
I have a little problem with a simple vbScript. The script has to run
I have a little problem with some jquery and http://www.mikage.to/jquery/jquery_history_noc.html The function works great,
I have a little problem where I would like to insert a svn diff

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.