Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UnboundLocalError: local variable 'url_request' referenced before assignment [duplicate]

Tags:

python

Think I'm going nuts here.

url_request = 0

def somefunction():
    url_request+=1

if __name__ =='__main__':
    somefunction()

Gives me the UnboundLocalError. What important concept am I missing here?

like image 809
user1561108 Avatar asked Dec 10 '12 09:12

user1561108


People also ask

How do you fix UnboundLocalError local variable referenced before assignment?

The Python "UnboundLocalError: Local variable referenced before assignment" occurs when we reference a local variable before assigning a value to it in a function. To solve the error, mark the variable as global in the function definition, e.g. global my_var .

How do I fix local variable might be referenced before assignment?

The UnboundLocalError: local variable referenced before assignment error is raised when you try to assign a value to a local variable before it has been declared. You can solve this error by ensuring that a local variable is declared before you assign it a value.

How do you prevent UnboundLocalError?

UnboundLocalError can be solved by changing the scope of the variable which is complaining. You need to explicitly declare the variable global. Variable x's scope in function printx is global. You can verify the same by printing the value of x in terminal and it will be 6.

How do you declare a local variable in Python?

In Python or any other programming languages, the definition of local variables remains the same, which is “A variable declared inside the function is called local function”. We can access a local variable inside but not outside the function.


2 Answers

You are assigning to a global variable, which means you need to mark it as a global:

def somefunction():
    global url_request
    url_request+=1

When you assign to a variable in a local scope, it is assumed to be a local variable unless you use a global statement to tell python otherwise first.

like image 95
Martijn Pieters Avatar answered Oct 21 '22 07:10

Martijn Pieters


For Python 2.7 we have to types of variables: global, local. Each function create it's own local scope for variables.

From local scope you can read without any restrictions. You can also call global object methods, so you can modify variable from global. But you can't reassign value.

Look at this code:

requests = [1,2,3]

def modify():
    requests.append(4)

def redeclare():
    requests = [10,20,30]

modify()
print requests
# will give you [1,2,3,4]

redeclare()
print requests
# will give you [1,2,3,4]

What's going on? You can't reassign requests variable from local scope, so interpreter create for you other variable - in local scope for redeclare call context.

Regarding to your code... Firstly, you try to reassign variable from global scope. Why? url_request is int, int is immutable, so operation url_request+=1 doesn't change value, it should reassign new value to variable name. Secondly, you don't specify global identify for this variable. So only one option for interpreter - to assume url_request as local variable. But... You didn't declare it's value anywhere....

UnboundLocalError means that you try to perform operations with variable value without declaring it before. Hope this will help you to understand more about Python variables/names/scopes.

like image 32
Alexey Kachayev Avatar answered Oct 21 '22 07:10

Alexey Kachayev