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 309751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:46:14+00:00 2026-05-12T07:46:14+00:00

I have a trivial example: def func1(): local_var = None def func(args): print args,

  • 0

I have a trivial example:


def func1():
    local_var = None

    def func(args):
        print args,
        print "local_var:", local_var

        local_var = "local"

    func("first")
    func("second")

func1()

I expect the output to be:

first local_var: None
second local_var: local

However, my actual output is:

first local_var:
Traceback (most recent call last):
  File "test.py", line 13, in 
    func1()
  File "test.py", line 10, in func1
    func("first")
  File "test.py", line 6, in func
    print "local_var:", local_var
UnboundLocalError: local variable 'local_var' referenced before assignment

My understanding of python scoping rules dictate that this should work as expected. I have other code where this works as expected, but reducing one non-working code fragment to it’s trivial case above also doesn’t work. So I’m stumped.

  • 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-12T07:46:14+00:00Added an answer on May 12, 2026 at 7:46 am

    The assignment to local_var in func makes it local to func — so the print statement references that “very very local” variable before it’s ever assigned to, as the exception says. As jtb says, in Python 3 you can solve this with nonlocal, but it’s clear from your code, using print statements, that you’re working in Python 2. The traditional solution in Python 2 is to make sure that the assignment is not to a barename and thus does not make the variable more local than you wish, e.g.:

    def func1():
        local_var = [None]
    
        def func(args):
            print args,
            print "local_var:", local_var[0]
    
            local_var[0] = "local"
    
        func("first")
        func("second")
    
    func1()
    

    the assignment to the indexing is not to a barename and therefore doesn’t affect locality, and since Python 2.2 it’s perfectly acceptable for nested inner functions to refer to variables that are locals in outer containing functions, which is all this version does (assigning to barenames is a different issue than referring to variables).

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

Sidebar

Related Questions

First: I'm shocked that I have to ask this question. Nowhere in the docs
I have a generated file with thousands of lines like the following: CODE,XXX,DATE,20101201,TIME,070400,CONDITION_CODES,LTXT,PRICE,999.0000,QUANTITY,100,TSN,1510000001 Some
I have a lot of content-heavy views in my ASP.NET MVC 2 site. These
I am new to both Python (and django) - but not to programming. I
I started to use silverlight/flex and immediately bumped into the asynchronous service calling. I'm
It looks that the old SqlHelper class from the Microsoft Enterprise Library has been
i am using rpy2-2.0.7 (i need this to work with windows 7, and compiling
Background Recently I've started to use XML a lot more as a column in
I may be mistaken, but the basic explanation I've found has been that the
I've got a question in my test: What is wrong with program that counts

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.