Python Forum
UnboundLocalError: local variable referenced before assignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UnboundLocalError: local variable referenced before assignment
#1
Hi

Need some help here
Original code
def test1():
    test_list = []
    test_var = 0
    test_string1 = ""

    def test2():
        temp =2
        if temp > test_var:
            test_var = temp
        else:
            test_string = "fail"
        return test_string
    
    res = test2()
    print res

test1()
https://docs.python.org/3/faq/programming.html#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value

I used both global , nonlocal but both throws different error


def test1():
    test_list = []
    test_var = 0
    test_string1 = ""

    def test2():
        nonlocal test_var
        temp =2
        if temp > test_var:
            test_var = temp
        else:
            test_string = "fail"
        return test_string
    
    res = test2()
    print res

test1()
throws this error
nonlocal test_var ^
SyntaxError: invalid syntax



def test1():
    test_list = []
    test_var = 0
    test_string1 = ""

    def test2():
        global test_var
        temp =2
        if temp > test_var:
            test_var = temp
        else:
            test_string = "fail"
        return test_string
    
    res = test2()
    print res

test1()
throws this error ^
NameError: global name 'test_var' is not defined

Any suggestions ? I have gone through top 10 search results , all suggest to use nonlocal or global...
Reply
#2
This is topic with lot of subtlety. Some of the scenarios are discussed in this thread: Namespace and scope difference

I think that this boils down to this:

Quote:You should also understand difference between reference and assignment:

When you reference a variable in an expression, the Python interpreter will traverse the scope to resolve the reference in following order:

- current function’s scope
- any enclosing scopes (like containing functions)
- scope of the module that contains the code (global scope)
- built-in scope (that contains functions like int and abs)

If Python doesn't find defined variable with the referenced name, then a NameError exception is raised.

Assigning a value to a variable works differently. If the variable is already defined in the current scope, then it will just take on the new value. If the variable doesn’t exist in the current scope, then Python treats the assignment as a variable definition. The scope of the newly defined variable is the function that contains the assignment.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Assignment of non-existing class variable is accepted - Why? DrZ 6 502 Jul-13-2020, 03:53 PM
Last Post: deanhystad
  UnboundLocalError: local variable 'figure_perso' referenced before assignment mederic39 2 373 Jun-11-2020, 12:45 PM
Last Post: Yoriz
  UnBoundLocalError Seaninho 3 451 May-31-2020, 07:22 AM
Last Post: azajali43
  local variable 'marks' referenced before assignment Calli 3 374 May-25-2020, 03:15 PM
Last Post: Calli
  Variable assignment wierdness with Memory eoins 1 409 Mar-08-2020, 10:15 AM
Last Post: scidam
  local variable troubles yokaso 4 682 Oct-20-2019, 05:25 PM
Last Post: ichabod801
  accessing local variable outside class priyanka08 3 492 Sep-24-2019, 10:00 AM
Last Post: buran
  UnboundLocalError: local variable ' ' referenced before assignment d3fi 10 1,479 Sep-03-2019, 07:22 PM
Last Post: buran
  local variable referenced before assignment. samvup 3 1,562 Aug-26-2019, 07:07 PM
Last Post: buran
  'Exception Has occured: UnBoundLocalError' caston 1 662 Jun-12-2019, 02:33 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020