示例:
import user
class Thing(object):
def doSomething(self):
u = user.User(1)
print u.name
>> UnboundLocalError: local variable 'user' referenced before assignment
但这是可行的:
class Thing(object):
def doSomething(self):
import user
u = user.Use
有人能解释一下这是怎么回事吗
x = 10
def foo():
print "x in foo = ",x
if x: x = 8 -------------> mysterious line
foo()
print "x in main = ",x
在上面的代码中,如果我注释掉神秘的行(如果x: x= 8)
我得到了输出
x in foo = 10
x in main = 10
否则我会以错误告终
"UnboundLocalError:赋值前引用的局部变量'x‘“
为什么会这样呢?
我知道,只有在需要
我的代码如下所示。
def test():
print num1
print num
num += 10
if __name__ == '__main__':
num = 0
num1 = 3
test()
在执行上述python代码时,我得到以下输出。
3
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
File "<stdin>", line 2, in test
Unbou
取下面的代码示例
var = True
def func1():
if var:
print("True")
else:
print("False")
# var = True
func1()
这将像人们所期望的那样打印True。
但是,如果取消注释# var = True,则会得到错误
UnboundLocalError: local variable 'var' referenced before assignment
为什么写入变量会使其他可访问的变量不可访问?这种设计选择背
这行python代码的错误之处是:
daterange = [begin + timedelta(n) for n in range((end - begin).days)]
其中,begin和end是具有有效值的datetime.date对象。
我在Django视图中使用它来处理一些数据,但每次调用它所在的视图时,我都会得到以下错误,并突出显示前面提到的行:
UnboundLocalError at /url/of/error/creating/view/here/
local variable 'range' referenced before assignment
如果我
a = 10
def f():
print(1)
print(a) # UnboundLocalError raised here
a = 20
f()
当然,此代码会引发UnboundLocalError: local variable 'a' referenced before assignment。但是为什么在print(a)行中会出现这个例外呢?
如果解释器逐行执行代码(就像我想的那样),那么当到达print(a)时,它不会知道有什么问题;它只会认为a引用了全局变量。
因此,解释器似乎预先读取了整个函数,以确定是否使用a进行赋值。这里有记录吗?除了检查语法错
我不知道怎么解释这个案子,你自己试试吧:
x = 7
def f():
if False:
global x
print("global")
x += 1
f()
print(x)
在我看来,上面的代码应该会导致"UnboundLocalError:局部变量'x‘在赋值前引用“,但是它不仅有效,而且还改变了全局变量'x’。
# coding: utf-8
def func():
print 'x is', x
#x = 2 #if I add this line, there will be an error, why?
print 'Changed local x to', x
x = 50
func()
print 'Value of x is', x
我没有在func函数中添加global x,但是它仍然可以找到x是50,为什么?
当我在func函数中添加x=2行时,会出现一个错误(UnboundLocalError:
我只是刚接触python,我试图编写这个程序,但是有一个错误,我无法解决它。
我在互联网上看到了一些其他的程序,但是它们都有不同的逻辑。
这里我想的逻辑是,如果字符串a中的任何字母是元音,那么我将按原样存储在字符串b中,如果它是辅音,那么我将在它上追加三个字母。
我知道我把绳子搞错了,但我还是弄不清楚。
节目是:-
a = ""
b = ""
def translate(a):
for i in a:
if (i == 'a' or i == 'e' or i == 'i' or i =
我在python3.8脚本中遇到了这样一种情况,在从外部文件导入了一组变量之后,在某些函数中,它们可以被访问,而在其他函数中则不能。例如,在文件min_time_n_clicks中定义一个常数tm_constants.py,只需简单的下垂,
min_time_n_clicks=10
请原谅这不是一个可重复的示例,因为完整的代码很长,这是为了显示我所命中的内容:在一个函数check_scope中,我可以使用from tm_constants import *访问变量,而另一个函数check_for_limiting却看不到它们--它击中了
UnboundLocalError: local var
我有以下简单代码:
i = 1
j = 6
test = 99
def main():
### i = 1 ### uncommenting this solves the problem with i
while i <= j:
print("i: ", i, "test: ", test)
i += 2
main()
错误:
而i
在Python中,全局变量是如何工作的?我知道全局变量是邪恶的,我只是在做实验。
这在python中不起作用:
G = None
def foo():
if G is None:
G = 1
foo()
我得到一个错误:
UnboundLocalError: local variable 'G' referenced before assignment
我做错了什么?
myVar = int(input("What do you want to start out with? "))
mySubtactor = int(input("What do you want to be the subtracter? "))
def function():
choice = input("Do you want to take away? Please say yes or no. ")
if(choice == 'yes'):
print(myVar)
a = 17
def test():
print(a)
a = 13
print(a)
test()
这是一条错误消息。
Traceback (most recent call last):
File "sandbox.py", line 6, in <module>
test()
File "sandbox.py", line 3, in test
print(a)
UnboundLocalError: local variable 'a' referenced before assignmen
prev, prev_re = '', (None) # these are globals
def find(h, p='', re=None):
print h, p, re
#global prev, prev_re
if p == '' and prev == h: return prev_re
prev, prev_re = h, re
return re
print find ("abc")
导致此错误:
if p == '' and prev == h
如果在我的节目中遇到了一个奇怪的问题。如果将我的代码简化为:
def x(y="test"):
def xx():
if False:
y = "blubb"
print y
xx()
print y
x()
这将引发赋值错误之前引用的UnboundLocalError:。
如果我将代码修复为以下代码:
def x(y="test"):
def xx():
print y
xx()
print y
x()
我的代码又起作用了
gCharlie = 0
gJeff = 0
def Bob ():
Charlie = gCharlie
Jeff = gJeff
Number = int(input("Hello and Welcome to Charlie's Number Guessing Game. Enter a nonnegative number from 0 to 10 not counting 0. "))
这段代码在Python 3.2中给出了这个错误:
UnboundLocalError: local variable 'gCharlie