为什么在Python类定义中的一个生成器中获取这个NameError? [英] Why do I get this NameError in a generator within a Python class definition?
问题描述
在Python 3.5.0中,此代码:
a =(1,2)
class Foo ):
b =(3,4)
c = tuple((i,j)for b in a for a in a)
d = tuple j in b)
产生:
Traceback(最近一次调用):
文件genexprtest.py,第2行,在< module>
class Foo(object):
文件genexprtest.py,第5行,在Foo
d = tuple((i,j)for i in a for j in b)
文件genexprtest.py,第5行,在< genexpr>
d = tuple((i,j)for i in a for j in b)
NameError:name'b'未定义
为什么会收到这个错误?为什么我不在上一行得到这个错误?
我花了几年的时间进行实验, '得到这个错误。我不确定,但这解释了为什么它适用于 c
,而不是 d
。我希望这可以帮助你,评论如果你不同意:)
def Tuple(this):
$每次调用
print #这总是工作
try:
print(b)#这总是给出错误
,除了NameError:
print(... b未定义)
try:
return tuple(this)#这里只给出了d和e
的错误,除了NameError:
print(...无法使它成为一个元组)
a =(1,2)
class Foo(object):
b =(3,4)
c = Tuple在b中for a in a)
d = Tuple((i,j)for i in a for j in b)
e = Tuple((i,j,k)for i in a for j in b for(5,6))
f = b中的j的Tuple((i,j,k)for a in(5,6)for k in a)
print \\\
c:,c,\\\
d:,d,\\\
e:,e,\\\
f:,f)
Tuple()
函数时,<$>发生了什么事情:<$> c $ c> b 未定义,但总是定义a
。这解释了为什么你得到d
和e
的错误,但它不解释为什么即使
b
是'未定义' $b 循环是在整个东西被转换为元组之前计算的。例如,如果你试图这样做:
$ b我的理论:第一个
for
Tuple((a,b,c)for a in loop1,for b in loop2 for c in loop3)
类,它将首先计算c中的 ,然后它将移动到foo并计算循环2和3.
总结:
- 移动到元组函数
- 执行剩余的循环
- 如果第二个或第三个循环中的变量在类Foo中,
In Python 3.5.0 this code:
a = (1,2) class Foo(object): b = (3,4) c = tuple((i,j) for j in b for i in a) d = tuple((i,j) for i in a for j in b)
produces:
Traceback (most recent call last): File "genexprtest.py", line 2, in <module> class Foo(object): File "genexprtest.py", line 5, in Foo d = tuple((i,j) for i in a for j in b) File "genexprtest.py", line 5, in <genexpr> d = tuple((i,j) for i in a for j in b) NameError: name 'b' is not defined
Why do I get this error? And why do I not get this error on the previous line?
解决方案I spent ages experimenting and I have a theory about why you're getting this error. I'm not certain but this does explain why it works for
c
and not ford
. I hope this helps you, comment if you disagree :)def Tuple(this): print(a) # this always works try: print(b) # this always gives an error except NameError: print("...b is not defined") try: return tuple(this) # this only gives an error for d and e except NameError: print("...couldn't make it a tuple") a = (1,2) class Foo(object): b = (3,4) c = Tuple((i,j) for j in b for i in a) d = Tuple((i,j) for i in a for j in b) e = Tuple((i,j,k) for i in a for j in b for k in (5, 6)) f = Tuple((i,j,k) for j in b for i in (5, 6) for k in a) print("\nc:", c,"\nd:", d,"\ne:", e,"\nf:", f)
What happened: every time I called the
Tuple()
function,b
was not defined, buta
was always defined. This explains why you get an error ford
ande
but it doesn't explain whyc
andf
work even thoughb
is 'not defined'My theory: The first
for
loop is calculated before the whole thing is converted into a tuple. For example, if you tried to do this:Tuple((a, b, c) for a in loop1, for b in loop2 for c in loop3)
, in the Foo class it would calculatefor a in loop1
first, then it would move to the foo and calculate the loops 2 and 3.In summary:
- does first for loop
- moves to tuple function
- does the remaining loops
- the error occurs if a variable in the 2nd or 3rd loop is in class Foo
这篇关于为什么在Python类定义中的一个生成器中获取这个NameError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!