x = "xtop"
y = "ytop"
def func():
x = "xlocal"
y = "ylocal"
class C:
print x #xlocal of course
print y #ytop why? I guess output may be 'ylocal' or '1'
y = 1
print y #1 of course
func()
-
Why x and y are different here?
为什么x和y在这里有所不同?
-
If I replace
class C
with a function scope I will getUnboundLocalError: local variable 'y' referenced before assignment
,What is the difference between a class and a function in this situation?如果我用函数作用域替换C类,我将得到UnboundLocalError:在赋值之前引用局部变量'y',在这种情况下,类和函数之间有什么区别?
1 个解决方案
#1
3
The reason for this is because the scope of class C
is actually different than the scope of def func
- and the different defaulting behaviours of scopes that python has.
原因是因为C类的范围实际上与def函数的范围不同 - 以及python具有的范围的不同默认行为。
Here is basically how python looks for a variable
这里基本上是python如何查找变量
- Look in current scope
- If current scope doesn't have it -> use nearest enclosing scope
- If current scope has it, but not yet defined -> use global scope
- If current scope has it, and already defined -> use it
- else blow up
查看当前范围
如果当前范围没有 - >使用最近的封闭范围
如果当前范围有,但尚未定义 - >使用全局范围
如果当前范围有,并且已经定义 - >使用它
否则爆炸
(If you remove ytop
you get a NameError: name 'y' is not defined
)
(如果删除ytop,则会出现NameError:未定义名称'y')
So basically, when the interpreter looks at the following section of code it goes
所以基本上,当解释器查看下面的代码部分时,它就会出现
class C:
print(x) # need x, current scope no x -> default to nearest (xlocal)
print(y) # need y, current scope yes y -> default to global (ytop)
# but not yet defined
y = 1
print(y) # okay we have local now, switch from global to local
Consider the following scenarios
请考虑以下方案
1) class C:
print(x)
print(y)
>>> xlocal
>>> ylocal
2) class C:
y = 1
print(x)
print(y)
>>> xlocal
>>> 1
3) class C:
print(x)
print(y)
x = 1
>>> xtop
>>> ylocal
#1
3
The reason for this is because the scope of class C
is actually different than the scope of def func
- and the different defaulting behaviours of scopes that python has.
原因是因为C类的范围实际上与def函数的范围不同 - 以及python具有的范围的不同默认行为。
Here is basically how python looks for a variable
这里基本上是python如何查找变量
- Look in current scope
- If current scope doesn't have it -> use nearest enclosing scope
- If current scope has it, but not yet defined -> use global scope
- If current scope has it, and already defined -> use it
- else blow up
查看当前范围
如果当前范围没有 - >使用最近的封闭范围
如果当前范围有,但尚未定义 - >使用全局范围
如果当前范围有,并且已经定义 - >使用它
否则爆炸
(If you remove ytop
you get a NameError: name 'y' is not defined
)
(如果删除ytop,则会出现NameError:未定义名称'y')
So basically, when the interpreter looks at the following section of code it goes
所以基本上,当解释器查看下面的代码部分时,它就会出现
class C:
print(x) # need x, current scope no x -> default to nearest (xlocal)
print(y) # need y, current scope yes y -> default to global (ytop)
# but not yet defined
y = 1
print(y) # okay we have local now, switch from global to local
Consider the following scenarios
请考虑以下方案
1) class C:
print(x)
print(y)
>>> xlocal
>>> ylocal
2) class C:
y = 1
print(x)
print(y)
>>> xlocal
>>> 1
3) class C:
print(x)
print(y)
x = 1
>>> xtop
>>> ylocal