Python 3: Unboundlocalerror: Local Variable Referenced Before Assignment

Python 3: UnboundLocalError: local variable referenced before assignment

You can fix this by passing parameters rather than relying on Globals

def function(Var1, Var2): 
if Var2 == 0 and Var1 > 0:
print("Result One")
elif Var2 == 1 and Var1 > 0:
print("Result Two")
elif Var1 < 1:
print("Result Three")
return Var1 -= 1
function(1, 1)

python giving me UnboundLocalError: local variable referenced before assignment when trying to work with variables

According to Python documentation, if the compiler sees a variable assignment in a function/method (local scope), it will automatically mark that name as local and hence not consider any similarly named outside variables. That is why, when it sees that before assignment of the local variable it is used inside function for something else (to check a condition in your case), it will throw an error that you actually are trying to use a variable which has not been assigned yet (in local terms).

If you changed lives = lives - 1 to new_lives = lives - 1, then the compiler would treat lives as a global variable and not throw an Exception. But this would create more problems in your case. I suggest passing lives as an argument to the function - def boss(lives): and call it in your loop by passing lives boss(lives).

Local variable referenced before assignment?

When Python parses the body of a function definition and encounters an assignment such as

feed = ...

Python interprets feed as a local variable by default. If you do not wish for it to be a local variable, you must put

global feed

in the function definition. The global statement does not have to be at the beginning of the function definition, but that is where it is usually placed. Wherever it is placed, the global declaration makes feed a global variable everywhere in the function.

Without the global statement, since feed is taken to be a local variable, when Python executes

feed = feed + 1,

Python evaluates the right-hand side first and tries to look up the value of feed. The first time through it finds feed is undefined. Hence the error.

The shortest way to patch up the code is to add global feed to the beginning of onLoadFinished. The nicer way is to use a class:

class Page(object):
def __init__(self):
self.feed = 0
def onLoadFinished(self, result):
...
self.feed += 1

The problem with having functions which mutate global variables is that it makes it harder to grok your code. Functions are no longer isolated units. Their interaction extends to everything that affects or is affected by the global variable. Thus it makes larger programs harder to understand.

By avoiding mutating globals, in the long run your code will be easier to understand, test and maintain.

UnboundLocalError: local variable referenced before assignment #

Your error: UnboundLocalError: local variable 'list1' referenced before assignment was coming because you have not defined list1 in medianOfList(self) but you are trying to access it.

Here's my solution:

class Solution():
def __init__(self,list1,list2):
self.list1=list1
self.list2=list2
def medianOfList(self,list1,list2):
self.list1=list1
self.list2=list2
list1=list1+list2
l=len(list1)
sum=0
for i in range(l):
sum+=list1[i]
return sum/l

list1=[1,2]
list2=[3,100]
m=Solution(list1,list2)
m.medianOfList(list1,list2)

Python3 UnboundLocalError: local variable referenced before assignment

Without the internet, an exception will be raised on this line.

response = requests.get(url, headers=headers, cookies=cookies, allow_redirects=False)

That would make response undefined.
One way to resolve this is to move the if statement to the try block

try:
response = requests.get(url, headers=headers, cookies=cookies, allow_redirects=False)
if "משתמש לא מזוהה" in response.text:
message = "User Was Logged Out Automatically!"
except requests.exceptions.RequestException as e:
message = "Connection Failed!"
run_again = True

UnboundLocalError: local variable <function> referenced before assignment

Whether a name used in a function is a global variable or a local one is determined at compile time, not at run time. Your functions that cause exceptions are trying to have it both ways, to either access a global, or provide their own local variable replacement, but Python's scoping rules don't allow that. It needs to be local or global, and can't be in a nebulous either/or state.

In your Test 1, the function raises an exception because the compiler saw that the code could assign to take_sum as a local variable, and so it makes all the references to take_sum in the code be local. You can no longer look up the global variable take_sum in the normal way once that determination has been made.

A global statement is in effect a compiler directive to change the assumption that an assignment makes a variable local. Subsequent assignments will be made globally, not locally. It's not something that executes at runtime, which is why your other two test cases are so confusing to you.

Test 2 fails because you're trying to tell the compiler that take_sum is a global after it has already seen some of your code make a local assignment to that name. In Test 3, the global statement comes first, so it makes the assignment (in the other branch!) assign to a global variable. It doesn't actually matter that the global statement was in a different branch than the assignment, the compiler interprets the global statement at compile time, not at runtime when the conditional logic of the ifs and elifs gets handled.

It might help your understanding of what is going on to disassemble some of the main functions you've written using the dis.dis function in the standard library. You'll see that there are two different sets of bytecodes used for loading and storing of variables, LOAD_GLOBAL/STORE_GLOBAL for global variables (used in all your functions to get names like print and globals), and LOAD_FAST/STORE_FAST which are used for local variables (like a, b and c in take_sum). The compiler behavior I talked about above boils down to which bytecode it chooses for each lookup or assignment.

If I rename the main function in Test 1 to test1, here's what I get when I disassemble it:

dis.dis(test1)
2 0 LOAD_CONST 1 ('take_sum')
2 LOAD_GLOBAL 0 (globals)
4 CALL_FUNCTION 0
6 CONTAINS_OP 1
8 POP_JUMP_IF_FALSE 18

3 10 LOAD_CONST 2 (<code object <lambda> at 0x0000019022A05F50, file "<ipython-input-23-0cc3c65f7038>", line 3>)
12 LOAD_CONST 3 ('test1.<locals>.<lambda>')
14 MAKE_FUNCTION 0
16 STORE_FAST 0 (take_sum)

5 >> 18 LOAD_GLOBAL 1 (print)
20 LOAD_FAST 0 (take_sum)
22 CALL_FUNCTION 1
24 POP_TOP
26 LOAD_CONST 0 (None)
28 RETURN_VALUE

Disassembly of <code object <lambda> at 0x0000019022A05F50, file "<ipython-input-23-0cc3c65f7038>", line 3>:
3 0 LOAD_FAST 0 (a)
2 LOAD_FAST 1 (b)
4 BINARY_ADD
6 LOAD_FAST 2 (c)
8 BINARY_ADD
10 RETURN_VALUE

Notice that the lookup of take_sum on line 5 is on byte 20 in the bytecode, where it uses LOAD_FAST. This is the bytecode that causes the UnboundLocalError, since there has been no local assigned if the global function exists.

Now, lets look at Test 3:

dis.dis(test3)
2 0 LOAD_CONST 1 ('take_sum')
2 LOAD_GLOBAL 0 (globals)
4 CALL_FUNCTION 0
6 CONTAINS_OP 0
8 POP_JUMP_IF_FALSE 12

3 10 JUMP_FORWARD 18 (to 30)

5 >> 12 LOAD_CONST 1 ('take_sum')
14 LOAD_GLOBAL 0 (globals)
16 CALL_FUNCTION 0
18 CONTAINS_OP 1
20 POP_JUMP_IF_FALSE 30

6 22 LOAD_CONST 2 (<code object <lambda> at 0x0000019022A43500, file "<ipython-input-26-887b66de7e64>", line 6>)
24 LOAD_CONST 3 ('test3.<locals>.<lambda>')
26 MAKE_FUNCTION 0
28 STORE_GLOBAL 1 (take_sum)

8 >> 30 LOAD_GLOBAL 2 (print)
32 LOAD_GLOBAL 1 (take_sum)
34 CALL_FUNCTION 1
36 POP_TOP

9 38 LOAD_GLOBAL 2 (print)
40 LOAD_GLOBAL 1 (take_sum)
42 LOAD_CONST 4 (1)
44 LOAD_CONST 5 (2)
46 LOAD_CONST 6 (3)
48 CALL_FUNCTION 3
50 CALL_FUNCTION 1
52 POP_TOP
54 LOAD_CONST 0 (None)
56 RETURN_VALUE

Disassembly of <code object <lambda> at 0x0000019022A43500, file "<ipython-input-26-887b66de7e64>", line 6>:
6 0 LOAD_FAST 0 (a)
2 LOAD_FAST 1 (b)
4 BINARY_ADD
6 LOAD_FAST 2 (c)
8 BINARY_ADD
10 RETURN_VALUE

This time the lookup of take_sum happens on bytecode 40, and it's a LOAD_GLOBAL (which succeeds since there is a global variable of that name).

Why am I getting UnboundLocalError local variable 'coffee_machine' referenced before assignment despite coffee_machine being a global variable?

You have not declared global coffee_machine at the start of the function, and thus it's not forced to be global, and within the function you try setting a value to it, which makes it local.

All that's needed to be done is adding that global line which will force it to be global, like so:

coffee_machine = True

def user_input():
global coffee_machine
while coffee_machine:
user_choice = input("What type of coffee would you like espresso/latte/cappuccino?")
if user_choice == "off".lower():
coffee_machine = False
x = []
for ingredients in MENU[user_choice].get("ingredients").values():
x.append(ingredients)
print(x)

user_input()

UnboundLocalError: local variable <var> referenced before assignment

I think you are searching for line where tenant == "tenant" and corresponding api_key and secret_key. Assuming there is only one tenant == "tenant" you can directly return accessKey, secretKey when it is found.

def keys(tenant):
for line in key_file[0]:
if tenant == line.get('tenant'):
accessKey = line.get('api_key')
secretKey = line.get('secret_key')
return accessKey, secretKey

I recreated your error in following code, which proves that variables declared inside conditional statements (like if, elif) are not accessible outside scope of that conditional statements.

# code 1
def func(a):
for i in a:
if i == '5':
n = 5

return n

a = [1, 2, 3, 4, 5]
print(func(a))
# code 2
def func(a):
for i in a:
if i == '5':
n = 5

return n

a = [1, 2, 3, 4, 5]
print(func(a))

Both code 1 and code 2 gives following error, because we are accessing n outside if statement.

UnboundLocalError: local variable 'n' referenced before assignment


Related Topics



Leave a reply



Submit