给出以下代码:
def A() :
b = 1
def B() :
# I can access 'b' from here.
print( b )
# But can i modify 'b' here? 'global' and assignment will not work.
B()
A()
Run Code Online (Sandbox Code Playgroud)
对于B()函数变量中的代码,b在外部作用域中,但不在全局作用域中.是否可以b从B()函数内修改变量?当然我可以从这里读取它print(),但是如何修改呢?
我已经阅读了关于该主题的几乎所有其他问题,但我的代码仍然不起作用.
我想我错过了一些关于python变量范围的东西.
这是我的代码:
PRICE_RANGES = {
64:(25, 0.35),
32:(13, 0.40),
16:(7, 0.45),
8:(4, 0.5)
}
def get_order_total(quantity):
global PRICE_RANGES
_total = 0
_i = PRICE_RANGES.iterkeys()
def recurse(_i):
try:
key = _i.next()
if quantity % key != quantity:
_total += PRICE_RANGES[key][0]
return recurse(_i)
except StopIteration:
return (key, quantity % key)
res = recurse(_i)
Run Code Online (Sandbox Code Playgroud)
我明白了
"全局名称'_total'未定义"
我知道问题在于_total作业,但我不明白为什么.不recurse()应该访问父函数的变量?
有人可以向我解释一下我对python变量范围缺少什么吗?
假设我有以下python代码:
def outer():
string = ""
def inner():
string = "String was changed by a nested function!"
inner()
return string
Run Code Online (Sandbox Code Playgroud)
我想调用outer()来返回"String被嵌套函数改变了!",但我得到了"".我得出结论,Python认为该行string = "string was changed by a nested function!"是对inner()本地新变量的声明.我的问题是:如何告诉Python它应该使用outer()字符串?我不能使用global关键字,因为字符串不是全局的,它只是在外部范围内.想法?
我有这段代码:
#!/usr/bin/env python
def get_match():
cache=[]
def match(v):
if cache:
return cache
cache=[v]
return cache
return match
m = get_match()
m(1)
Run Code Online (Sandbox Code Playgroud)
如果我运行它,它说:
UnboundLocalError: local variable 'cache' referenced before assignment
Run Code Online (Sandbox Code Playgroud)
但如果我这样做:
#!/usr/bin/env python
def get():
y = 1
def m(v):
return y + v
return m
a=get()
a(1)
Run Code Online (Sandbox Code Playgroud)
它运行.
列表中有什么东西吗?或者我的代码组织错了?
为什么这样做:
def function1():
a = 10
def function2():
print a
function2()
Run Code Online (Sandbox Code Playgroud)
但这不是:
def function1():
a = 10
def function2():
print a
a -= 1
if a>0:
function2()
function2()
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
UnboundLocalError: local variable 'a' referenced before assignment
Run Code Online (Sandbox Code Playgroud) 在python中,我可以写:
def func():
x = 1
print x
x+=1
def _func():
print x
return _func
test = func()
test()
Run Code Online (Sandbox Code Playgroud)
当我运行它时,输出是:
1
2
由于_func可以访问func中定义的"x"变量.对...
但如果我这样做:
def func():
x = 1
print x
def _func():
x+=1
print x
return _func
test = func()
test()
Run Code Online (Sandbox Code Playgroud)
然后我收到一条错误消息:UnboundLocalError:在赋值之前引用的局部变量'x'
在这种情况下,似乎_func不能"看到""x"变量
问题是:为什么在第一个例子中打印x"看到""x"变量,而数学运算符x + = 1抛出异常?
我不明白为什么......
我希望它不是重复的(同时很难说,鉴于这些错误的问题数量,但这是基本的错误),但我不明白这里发生了什么.
def f():
c = ord('a')
f()
Run Code Online (Sandbox Code Playgroud)
运行,没有错误(ord将字符转换为ASCII代码,它是内置的).现在:
if False:
ord = None
def f():
c = ord('a')
f()
Run Code Online (Sandbox Code Playgroud)
也运行,没有错误(ord没有被覆盖,条件总是假的).现在:
def f():
if False:
ord = None
c = ord('a')
f()
Run Code Online (Sandbox Code Playgroud)
我得到(在哪里c = ord('a'))
UnboundLocalError: local variable 'ord' referenced before assignment
Run Code Online (Sandbox Code Playgroud)
似乎仅引用左侧操作数使其成为局部变量,即使代码未运行.
显然我可以解决这个问题,但我非常惊讶,因为python的动态方面允许你定义一个变量,就像是一个整数,并在下一行定义为一个字符串.
它似乎与在if语句中初始化的变量的范围有什么关系?
显然,解释器在编译为字节码时仍会记录未到达的分支,但究竟发生了什么?
(在Python 2.7和Python 3.4上测试)