python scoping and run order in building namespace [duplicate] - python

This question already has answers here:
UnboundLocalError trying to use a variable (supposed to be global) that is (re)assigned (even after first use)
(14 answers)
Closed 6 years ago.
Why does this not work in python?, where I can access the enclosing scope (here global scope) when there are no other references to the same variable but can't when there are.
Does the interpreter look further down the function first before for the variable definition, finds it so assumes it is only a local variable which hasn't yet been assigned a value? What's the run order of the interpreter here?
> a = 5
> a
Out[3]:
5
> def closure():
print(a)
> closure()
5
> def closure():
print(a)
a = "another"
return a
> closure()
UnboundLocalError: local variable 'a' referenced before assignment

Add global a to the first line of the function.

Related

How to understand global and local scopes in Python? [duplicate]

This question already has answers here:
UnboundLocalError trying to use a variable (supposed to be global) that is (re)assigned (even after first use)
(14 answers)
Using global variables in a function
(25 answers)
Closed 5 months ago.
I am a novice in Python and wondering the situation below.
x = 1
def func():
print(x)
x = 2
return x
So I got the UnboundLocalError: local variable 'x' referenced before assignment.
But if I right understand - Python read and execute code row by row.
So in first statement inside function "print(x)" it must just relay global variable x which eq. 1, but instead I got the error.
Please explain, I think it simple.
I think your problem was explained as well in the FAQ of python docs
This is because when you make an assignment to a variable in a scope,
that variable becomes local to that scope and shadows any similarly
named variable in the outer scope. Since the last statement in foo
assigns a new value to x, the compiler recognizes it as a local
variable. Consequently when the earlier print(x) attempts to print the
uninitialized local variable and an error results.

Why this statement produce a print error? [duplicate]

This question already has answers here:
UnboundLocalError trying to use a variable (supposed to be global) that is (re)assigned (even after first use)
(14 answers)
Local variable referenced before assignment? [duplicate]
(5 answers)
Closed 11 months ago.
def f():
print(x)
def g():
print(x)
x = 1
x = 3
f()
x = 3
g()
It prints 3 when f is invoked, but the error message
UnboundLocalError: local variable 'x' referenced before assignment
is printed when the print statement in g is encountered. This happens because
the assignment statement following the print statement causes x to be local to g.
And because x is local to g, it has no value when the print statement is executed.
The above quote and code are from 《Introduction to Computation and Programming Using Python》
And here is my understanding through this explanation. Just wondering if this is valid:
The print statement in function f does not produce an error because it does not contain the name of x as a local variable, forcing the interpreter to search the stack frame with scope outside the definition of x and finding out that x binds to 3. Then use the value. While, in function g, there exists a local variable binds to number 1 after the print statement, the function already takes x as a local variable as x binds to some values in the function body. The interpreter does not search back to other stack frames with scope, hence the outer x bound will not be searched and used. Based on this, it has no value in x yet when the print statement is executed.

How to set variable within function def in python? [duplicate]

This question already has answers here:
UnboundLocalError trying to use a variable (supposed to be global) that is (re)assigned (even after first use)
(14 answers)
UnboundLocalError: local variable … referenced before assignment [duplicate]
(2 answers)
Closed 2 years ago.
I have a simple code like this:
temp = 100
def my_fun():
temp += 10
return temp
After I call this function my_fun() I get an error
local variable 'temp' referenced before assignment
What's wrong with my code? I set temp before I call it. But still an error.
Thanks to all.
Best would be to pass it in:
def my_fun(temp):
temp += 10
return temp
temp = 100
temp = my_fun(temp)
But if you really want to access it from the outer scope, then use:
global temp
Edit: I see you amended your question - the reason for the error is the scope of the var. Within the function that variable only exists at the function level, and since you've not actually assigned it or passed it in, it doesn't exist when you try and increment it.

Scope of variable and list in python [duplicate]

This question already has answers here:
UnboundLocalError trying to use a variable (supposed to be global) that is (re)assigned (even after first use)
(14 answers)
Is it possible to modify a variable in python that is in an outer (enclosing), but not global, scope?
(9 answers)
Closed 3 years ago.
I am curious why the following code generates error:
def test_method():
main_var = 0
main_list = list()
def sub_method():
print(main_list)
main_list.append(0)
print(main_list)
print(main_var)
main_var +=1
print(main_var)
sub_method()
Error
UnboundLocalError: local variable 'main_var' referenced before assignment
In particular, it does not complain about list, but does complain about variable.

Assigning a value to a global variable inside a function [duplicate]

This question already has answers here:
UnboundLocalError trying to use a variable (supposed to be global) that is (re)assigned (even after first use)
(14 answers)
Closed 4 years ago.
a = 2
def alter_a():
a = a * 2
return a
Why doesn't this work? I know it won't change the value of the global variable, but can't it work inside the function?
Sssign a new value to a... which is the old value * 2. Why isn't this possible?
The a inside the function lives inside the function scope, if you want to reference the outer one use global:
a = 2
def alter_a():
global a
a = a * 2

Categories