如何在函数中创建或使用全局变量?
如果我在一个函数中创建一个全局变量,我如何在另一个函数中使用该全局变量?我是否需要将全局变量存储在需要访问的函数的局部变量中?
以下代码在Python 2.5和3.0中按预期工作:
a, b, c = (1, 2, 3)
print(a, b, c)
def test():
print(a)
print(b)
print(c) # (A)
#c+=1 # (B)
test()
Run Code Online (Sandbox Code Playgroud)
但是,当我取消注释行(B)时,我得到了UnboundLocalError: 'c' not assigned一行(A).的值a和b被正确地打印.这让我感到困惑,原因有两个:
为什么在行(A)处抛出运行时错误,因为后面的行(B)语句?
为什么变量a和b打印符合预期,同时c引发错误?
我能想到的唯一解释是,赋值创建了一个局部变量,即使在创建局部变量之前,它也优先于"全局"变量.当然,变量在存在之前"窃取"范围是没有意义的.cc+=1c
有人可以解释一下这种行为吗?
我想弄清楚这个:
c = 1
def f(n):
print c + n
def g(n):
c = c + n
f(1) # => 2
g(1) # => UnboundLocalError: local variable 'c' referenced before assignment
Run Code Online (Sandbox Code Playgroud)
谢谢!
对于以下Python 2.7代码:
#!/usr/bin/python
def funcA():
print "funcA"
c = 0
def funcB():
c += 3
print "funcB", c
def funcC():
print "funcC", c
print "c", c
funcB()
c += 2
funcC()
c += 2
funcB()
c += 2
funcC()
print "end"
funcA()
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
File "./a.py", line 9, in funcB
c += 3
UnboundLocalError: local variable 'c' referenced before assignment
Run Code Online (Sandbox Code Playgroud)
但是,当我注释掉该行c += 3中funcB,我得到以下的输出:
funcA
c 0
funcB 0
funcC 2
funcB 4
funcC 6
end
Run Code Online (Sandbox Code Playgroud)
c在 …
我正在尝试添加或减去已定义的变量,但我无法弄清楚如何使用新值覆盖旧值.
a = 15
def test():
a = a +10
print ( a )
test()
Run Code Online (Sandbox Code Playgroud)
错误信息:
Traceback (most recent call last):
File "test.py", line 7, in <module>
test()
File "test.py", line 4, in test
a = a +10
UnboundLocalError: local variable 'a' referenced before assignment
Run Code Online (Sandbox Code Playgroud) 我想我在这里疯了.
url_request = 0
def somefunction():
url_request+=1
if __name__ =='__main__':
somefunction()
Run Code Online (Sandbox Code Playgroud)
给我UnboundLocalError.我在这里错过了什么重要概念?
当我尝试编译下面的代码时,我得到了这个错误
UnboundLocalError: local variable 'L' referenced before assignment
Run Code Online (Sandbox Code Playgroud)
有人可以解释原因吗?是不是之前分配的全局变量?
我的Python版本是2.7.3
#!/usr/bin/env python
import pygame
from pygame.locals import *
from sys import exit
import random
import math
R = int(8) # promien planety
N = 5 # liczba planet
G = 2 # stala "grawitacyjna"
L = 1
def compute_dv(p1,p2):
dx = p2[0]-p1[0]
dy = p2[1]-p1[1]
r = math.hypot(dx,dy)
dx /= r*r
dy /= r*r
if(L>1000):
print "r= ", r, "dx= ", dx, "dy= ", dy, "dx/ r*r = ", dx, …Run Code Online (Sandbox Code Playgroud) 所以基本上我不知道这小块代码有什么问题,而且似乎找不到让它工作的方法.
points = 0
def test():
addpoint = raw_input ("type ""add"" to add a point")
if addpoint == "add":
points = points + 1
else:
print "asd"
return;
test()
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
UnboundLocalError: local variable 'points' referenced before assignment
Run Code Online (Sandbox Code Playgroud)
注意:我不能在函数中放置"points = 0",因为我会重复多次,所以它总是先将点设置回0.我完全陷入困境,任何帮助将不胜感激!
有人可以用Python解释一下下面的结果吗?
当运行以下代码片段时,Python 会抛出错误,指出该变量x在赋值之前被引用:
x = 1
def increase_x():
x += 1
increase_x()
Run Code Online (Sandbox Code Playgroud)
当然,解决方案是global x在 的函数声明之后包含该行increase_x。
但是,当运行下一段代码时,没有错误,结果正如您所期望的:
x = [2, -1, 4]
def increase_x_elements():
for k in range(len(x)):
x[k] += 1
increase_x_elements()
Run Code Online (Sandbox Code Playgroud)
这是因为整数在Python中是基元(而不是对象),所以x在第一个片段中是存储在内存中的基元,而x在第二个片段中引用了指向列表对象的指针?
这是我的代码:
x = 1
def poi(y):
# insert line here
def main():
print poi(1)
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
如果放置4条线,一次一条,代替 # insert line here
Lines | Output
---------------+--------------
1. return x | 1
2. x = 99 |
return x | 99
3. return x+y | 2
4. x = 99 | 99
Run Code Online (Sandbox Code Playgroud)
在上面的行中,似乎在情况1和3中使用了在函数之上声明的全局x
但是,
x = x*y
return x
Run Code Online (Sandbox Code Playgroud)
这给了
error : local variable 'x' is reference before assignment
Run Code Online (Sandbox Code Playgroud)
这里有什么问题?
python ×10
scope ×4
python-2.7 ×2
function ×1
global ×1
pointers ×1
python-3.x ×1
variables ×1