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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:25:50+00:00 2026-06-16T21:25:50+00:00

Possible Duplicate: referenced before assignment error in python I’m getting a strange error in

  • 0

Possible Duplicate:
referenced before assignment error in python

I’m getting a strange error in python. The following ipython log sums it up:

In [10]: def confused(stuff):
   ....:     print huh
   ....:     return stuff
   ....: 

In [11]: confused(87)
0
Out[11]: 87

In [12]: def confused(stuff):
   ....:     print huh
   ....:     huh += 1
   ....:     return stuff
   ....: 

In [13]: confused(9)
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
/home/max/verk/btr-email/build/x86_64/bin/ipython in <module>()
----> 1 confused(9)

/home/max/verk/btr-email/build/x86_64/bin/ipython in confused(stuff)
      1 def confused(stuff):
----> 2     print huh
      3     huh += 1
      4     return stuff

UnboundLocalError: local variable 'huh' referenced before assignment

The only difference between the function that works and the one that throws an error is the +=1 line, and even then, it throws an error on a line which was previously working! It also doesn’t throw an error if I put global huh before referencing huh in the 2nd version of the method.

Why does adding a line where I add one to the variable suddenly change it from a global to a local variable?

  • 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-06-16T21:25:51+00:00Added an answer on June 16, 2026 at 9:25 pm

    In your script, huh refers to a global variable. You can’t change the reference to a global variable in a function without explicitly telling python that is what you want to do:

    def confused(stuff):
        global huh
        print huh
        huh += 1
        return stuff
    

    For immutable objects like ints, strings, floats, etc, that means that you can’t make any changes to the object without declaring it as global. For mutable objects, you can make changes to the objects items or attributes, but you still can’t change the object’s reference.


    This is all a question of scope. since huh isn’t in the local scope of confused, python finds it in the global scope. Since it is found in the global scope, you can’t assign to it unless you specifically say that you want to (using global as I have done above). However, if it’s a list, once the list is found, you have access to all of that list’s methods (including __setitem__, append, etc.)


    As to the location of the error, this can be cleared up with a little disassembling:

    >>> def confused(stuff):
    ...    print huh
    ... 
    
    >>> import dis
    >>> dis.dis(confused)
      2           0 LOAD_GLOBAL              0 (huh)
                  3 PRINT_ITEM          
                  4 PRINT_NEWLINE       
                  5 LOAD_CONST               0 (None)
                  8 RETURN_VALUE
    
    >>> def confused2(stuff):
    ...    print huh
    ...    huh += 1
    ... 
    
    >>> dis.dis(confused2)
      2           0 LOAD_FAST                1 (huh)
                  3 PRINT_ITEM          
                  4 PRINT_NEWLINE       
    
      3           5 LOAD_FAST                1 (huh)
                  8 LOAD_CONST               1 (1)
                 11 INPLACE_ADD         
                 12 STORE_FAST               1 (huh)
                 15 LOAD_CONST               0 (None)
                 18 RETURN_VALUE   
    

    You can see that in confused2, python is already trying to LOAD_FAST (meaning, look for a local variable) at the first line of the function. However, no local variable huh exists hence the Exception.

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

Sidebar

Related Questions

Possible Duplicate: referenced before assignment error in python So I've started to try to
Possible Duplicate: Python nested functions variable scoping After much trial and error I have
Possible Duplicate: Read/Write Python Closures In the following function, the internal function does not
Possible Duplicate: Modify the function variables frominner function in python Say I have this
Possible Duplicate: non static method cannot be referenced from static context hey i have
Possible Duplicate: What is an undefined reference/unresolved external symbol error and how do I
Possible Duplicate: How are DLLs loaded by the CLR? In .NET are referenced DLLs
Possible Duplicate: What is an undefined reference/unresolved external symbol error and how do I
Possible Duplicate: What is an undefined reference/unresolved external symbol error and how do I
Possible Duplicate: Strange behavior Of foreach Strange behaviour after loop by reference - Is

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.