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 7 years ago.
I would like to use function of same name from different packages dependent on a function flag:
import chainer.functions as F
def random_q_z(input, test = False):
if test:
F = np
# ...
else:
# ...
pass
return F.sum(input)
However the interpreter protests:
UnboundLocalError: local variable 'F' referenced before assignment
How to please it and do conditional referencing of packages?
I see that this question relates to other questions on variable scopes, but here the question is about how to handle different scopes. And the answer I got is valuable for this particular question.
Make F a default parameter:
import chainer.functions as F
def random_q_z(input, test=False, F=F):
if test:
F = np
return F.sum(input)
If you don't provide F as an argument when calling random_q_z, chainer.functions is used. You can also give random_q_z a different function for F.
Related
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.
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.
This question already has answers here:
How to get local variables updated, when using the `exec` call?
(3 answers)
Setting variables with exec inside a function
(2 answers)
Closed 3 years ago.
I have a function with an unspecified number of input strings which are intended to be variable names. For example
def f(*args):
for arg in args:
exec('{} = 1'.format(arg))
return a
f('a', 'b')
When running the code, I recieve the following error
NameError: name 'a' is not defined
How do I assign local variables for the function which are to be manipulated or returned? The solution provided in this similar but different question requires creating the variables outside the function, i.e. adding them to the global namespace, but that is not what I want.
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 9 years ago.
I'm trying to do something along the following lines in python3:
i = 1337
def g():
print(i)
i = 42
g()
but get the following error
UnboundLocalError: local variable 'i' referenced before assignment
I think I understand what the error message means, but why is it the case? Is there any way to circumvent this?
In two words - when a given variable name is not assigned a value within a function then references to the variable will be looked up. Use global - and in such a case python will look for i in global scope:
i = 1337
def g():
global i
print i
i = 42
g()
You can read more on variable scopes in PEP-0227
If you really really want to do it that way, you'll need to get i from the global scope.
def g():
global i
print i
i = 42
However, generally you would be much better off changing how your code works to not require globals. Depending on how you are using it, that could be as simple as passing in i as a parameter and returning the changed value.
An example of Keeyai's suggestion of passing in i as a parameter:
i = 1337
def g(i):
print(i)
i = 42
g(i)
However, you never use the new value of i, so perhaps something like this makes more sense:
def g (i):
i = 42
return i
i = 1337
print g(i)
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 9 years ago.
I'm having an issue with variable and function. Here is a simple code:
r = 0
list = ['apple','lime','orange']
def list_list(x):
for i in x:
r +=1
print r
list_list(list)
Error:
UnboundLocalError: local variable 'r' referenced before assignment
I know it must be something simple. I started to do my script using functions instead straight code.
You should rewrite your function to take r as an argument if you want to define it outside of your function:
def my_func(some_list, r=0):
# do some stuff
Basically, you have a problem with scope. If you need r outside of the function, just return it's value in a tuple:
def my_func(some_list, r=0):
# do some stuff
return new_list, r
my_list = [1,2,3,4,5]
different_list, my_outside_r = my_func(some_list, 0)
The r within the function isn't the same as the one outside the function, so it hasn't been set yet.
You shoudld put r = 0 inside the function. But if you want the length of the list just use len(list)
Also try to avoid naming variables same as builtin names like list.