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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:25:41+00:00 2026-05-24T05:25:41+00:00

def Test(value): def innerFunc(): print value innerFunc() def TestWithAssignment(value): def innerFunc(): print value value

  • 0
def Test(value):
    def innerFunc():
        print value
    innerFunc()

def TestWithAssignment(value):
    def innerFunc():
        print value
        value = "Changed value"
    innerFunc()

Test("Hello 1")
# Prints "Hello 1"

TestWithAssignment("Hello 2")
# Throws UnboundLocalError: local variable 'value' referenced before assignment
# on the "print value" line

Why does the second one fail, given that the only difference is an assignment which comes after the print statement? I am pretty confused about this.

  • 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-24T05:25:42+00:00Added an answer on May 24, 2026 at 5:25 am

    The issue is that Python wants you to be explicit and you want to be implicit. The execution model that Python uses binds names to the nearest available enclosing scope.

    def Test(value):
        # Local Scope #1
        def innerFunc():
            # Local Scope #2
            print value 
            # No immediate local in Local Scope #2 - check up the chain
            # First find value in outer function scope (Local Scope #1).
            # Use that.
        innerFunc()
    
    def TestWithAssignment(value):
        # Local Scope #1
        def innerFunc():
            # Local Scope #2
            print value 
            # Immediate local variable found in Local Scope #2.
            # No need to check up the chain.
            # However, no value has been assigned to this variable yet.
            # Throw an error.
            value = "Changed value"
        innerFunc()
    

    There is not (as far as I know) a way to walk up the scope in Python 2.x – you have globals() and locals() – but any scopes between the global and the local scope cannot be accessed (if this is not true, I’d love to be corrected).

    However, you can pass the local variable value into your inner local scope:

    def TestWithAssignment(value):
        def innerFunc(value):
            print value 
            # Immediate local **and assigned now**.
            value = "Changed value"
            # If you need to keep the changed value
            # return value
        innerFunc(value)
        # If you need to keep the changed value use:
        # value = innerFunc(value)
    

    In Python 3 you have the new nonlocal statement that can be used to refer to the containing scope (Thanks @Thomas K).

    def TestWithAssignment(value):
        def innerFunc():
            nonlocal value
            print value 
            value = "Changed value"
        innerFunc()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code: class MyClass(): def test(self): self.__x = 0 def __setattr__(self, name, value):
I have this test case def setUp(self): self.user = User.objects.create(username=tauri, password='gaul') def test_loginin_student_control_panel(self): c
I need some like this: module One def test; puts 'Test One'; end end
How can it be that this test case import unittest class PropTest(unittest.TestCase): def test(self):
When I feed a utf-8 encoded xml to an ExpatParser instance: def test(filename): parser
When I try this code: a, b, c = (1, 2, 3) def test():
I'm trying to write basic assert test: def assert_session_has ( sessionvar ) return assert_not_nil
i have the following class test hash={} def printHash puts hash[1] puts hash[2] puts
I get this error object has no attribute 'im_func' with this class Test(object): def
I have a variable in my Grails app’s BootStrap.groovy : class BootStrap { def

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.