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 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

Ask A Question

Stats

  • Questions 213k
  • Answers 213k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer for index, s in enumerate(stocks_list): print index, s May 12, 2026 at 10:30 pm
  • Editorial Team
    Editorial Team added an answer No, there's no easy way to do this using Java's… May 12, 2026 at 10:30 pm
  • Editorial Team
    Editorial Team added an answer If the team is floundering at implementing TDD, but they… May 12, 2026 at 10:30 pm

Related Questions

I know this must be a trivial question, but I've tried many different ways,
I have a function that has several outputs, all of which native, i.e. integers
I would like to do something like the following: class String def fancy_thing appendix
Questions: What is the best practice for keeping track of a thread's progress without

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.