25

在 Python 3 中运行此代码后:

import pdb

def foo():
    nums = [1, 2, 3]
    a = 5
    pdb.set_trace()

foo()

以下表达式有效:

(Pdb) print(nums)
[1, 2, 3]

(Pdb) print(a)
5

(Pdb) [x for x in nums]
[1, 2, 3]

但以下表达式失败:

(Pdb) [x*a for x in nums]
*** NameError: global name 'a' is not defined

以上在 Python 2.7 中运行良好。

这是一个错误还是我错过了什么?

更新:查看新接受的答案。这确实是一个错误(或有问题的设计),现在已通过在 pdb 中引入新的命令和模式来解决。

4

3 回答 3

25

如果您输入interact[i]pdb 会话,您将获得一个交互式会话,并且列表推导在此模式下按预期工作

来源:http ://bugs.python.org/msg215963

于 2016-06-03T22:48:46.497 回答
10

它工作得很好:

>>> import pdb
>>> def f(seq):
...     pdb.set_trace()
... 
>>> f([1,2,3])
--Return--
> <stdin>(2)f()->None
(Pdb) [x for x in seq]
[1, 2, 3]
(Pdb) [x in seq for x in seq]
[True, True, True]

如果不显示您实际在做什么,没有人可以告诉您为什么在您的特定情况下您会得到一个NameError.


TL;DR在 python3 中,list-comprehensions 实际上是具有自己堆栈帧的函数,您无法从内部堆栈帧访问seq变量,它是 的参数。test相反,它被视为全局(因此,未找到)。


你看到的是 python2 和 python3 中列表理解的不同实现。在 python 2 中,list-comprehensions 实际上是for循环的简写,您可以在字节码中清楚地看到这一点:

>>> def test(): [x in seq for x in seq]
... 
>>> dis.dis(test)
  1           0 BUILD_LIST               0
              3 LOAD_GLOBAL              0 (seq)
              6 GET_ITER            
        >>    7 FOR_ITER                18 (to 28)
             10 STORE_FAST               0 (x)
             13 LOAD_FAST                0 (x)
             16 LOAD_GLOBAL              0 (seq)
             19 COMPARE_OP               6 (in)
             22 LIST_APPEND              2
             25 JUMP_ABSOLUTE            7
        >>   28 POP_TOP             
             29 LOAD_CONST               0 (None)
             32 RETURN_VALUE        

请注意字节码如何包含FOR_ITER循环。另一方面,在 python3 中,list-comprehension 实际上是具有自己堆栈框架的函数:

>>> def test(): [x in seq2 for x in seq]
... 
>>> dis.dis(test)
  1           0 LOAD_CONST               1 (<code object <listcomp> at 0xb6fef160, file "<stdin>", line 1>) 
              3 MAKE_FUNCTION            0 
              6 LOAD_GLOBAL              0 (seq) 
              9 GET_ITER             
             10 CALL_FUNCTION            1 
             13 POP_TOP              
             14 LOAD_CONST               0 (None) 
             17 RETURN_VALUE      

如您所见,这里没有FOR_ITER,而是有 aMAKE_FUNCTIONCALL_FUNCTION字节码。如果我们检查列表理解的代码,我们可以理解绑定是如何设置的:

>>> test.__code__.co_consts[1]
<code object <listcomp> at 0xb6fef160, file "<stdin>", line 1>
>>> test.__code__.co_consts[1].co_argcount   # it has one argument
1
>>> test.__code__.co_consts[1].co_names      # global variables
('seq2',)
>>> test.__code__.co_consts[1].co_varnames   # local variables
('.0', 'x')

.0是函数的唯一参数。x是循环的局部变量,seq2全局变量。请注意.0,list-comprehension 参数是从 获得的可迭代对象seq,而不是seq自身。(参见上面GET_ITER输出中的操作码dis)。使用更复杂的示例可以更清楚地说明这一点:

>>> def test():
...     [x in seq for x in zip(seq, a)]
... 
>>> dis.dis(test)
  2           0 LOAD_CONST               1 (<code object <listcomp> at 0xb7196f70, file "<stdin>", line 2>) 
              3 MAKE_FUNCTION            0 
              6 LOAD_GLOBAL              0 (zip) 
              9 LOAD_GLOBAL              1 (seq) 
             12 LOAD_GLOBAL              2 (a) 
             15 CALL_FUNCTION            2 
             18 GET_ITER             
             19 CALL_FUNCTION            1 
             22 POP_TOP              
             23 LOAD_CONST               0 (None) 
             26 RETURN_VALUE 
>>> test.__code__.co_consts[1].co_varnames
('.0', 'x')

在这里,您可以看到列表理解的唯一参数(始终由 表示.0)是从 获得的迭代zip(seq, a)seq并且a它们自己不会传递给列表理解。只有iter(zip(seq, a))在列表理解中传递。

我们必须做的另一个观察是,当你运行时pdb,你不能从你想要定义的函数中访问当前函数的上下文。例如,以下代码在 python2 和 python3 上均失败:

>>> import pdb
>>> def test(seq): pdb.set_trace()
... 
>>> test([1,2,3])
--Return--
> <stdin>(1)test()->None
(Pdb) def test2(): print(seq)
(Pdb) test2()
*** NameError: global name 'seq' is not defined

test2它失败是因为在定义seq变量时被视为全局变量,但它实际上是函数内部的局部变量test,因此无法访问。

您看到的行为类似于以下场景:

#python 2 no error
>>> class A(object):
...     x = 1
...     L = [x for _ in range(3)]
... 
>>> 

#python3 error!
>>> class A(object):
...     x = 1
...     L = [x for _ in range(3)]
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in A
  File "<stdin>", line 3, in <listcomp>
NameError: global name 'x' is not defined

第一个没有给出错误,因为它主要相当于:

>>> class A(object):
...     x = 1
...     L = []
...     for _ in range(3): L.append(x)
... 

由于列表理解在字节码中“扩展”了。在 python3 中它失败了,因为您实际上是在定义一个函数,并且您无法从嵌套函数范围访问类范围:

>>> class A(object):
...     x = 1
...     def test():
...             print(x)
...     test()
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in A
  File "<stdin>", line 4, in test
NameError: global name 'x' is not defined

请注意,genexp 在 python2 上被实现为函数,实际上您会看到与它们类似的行为(在 python2 和 python3 上):

>>> import pdb
>>> def test(seq): pdb.set_trace()
... 
>>> test([1,2,3])
--Return--
> <stdin>(1)test()->None
(Pdb) list(x in seq for x in seq)
*** Error in argument: '(x in seq for x in seq)'

这里pdb没有给你更多的细节,但是失败的原因是一样的。


总之:这不是一个错误,pdb而是 python 实现范围的方式。AFAIK 更改它以允许您尝试执行的操作pdb将需要对函数的处理方式进行一些重大更改,我不知道是否可以在不修改解释器的情况下完成此操作。


请注意,当使用嵌套列表理解时,嵌套循环在字节码中展开,就像 python2 中的列表理解一样:

>>> import dis
>>> def test(): [x + y for x in seq1 for y in seq2]
... 
>>> dis.dis(test)
  1           0 LOAD_CONST               1 (<code object <listcomp> at 0xb71bf5c0, file "<stdin>", line 1>) 
              3 MAKE_FUNCTION            0 
              6 LOAD_GLOBAL              0 (seq1) 
              9 GET_ITER             
             10 CALL_FUNCTION            1 
             13 POP_TOP              
             14 LOAD_CONST               0 (None) 
             17 RETURN_VALUE         
>>> # The only argument to the listcomp is seq1
>>> import types
>>> func = types.FunctionType(test.__code__.co_consts[1], globals())
>>> dis.dis(func)
  1           0 BUILD_LIST               0 
              3 LOAD_FAST                0 (.0) 
        >>    6 FOR_ITER                29 (to 38) 
              9 STORE_FAST               1 (x) 
             12 LOAD_GLOBAL              0 (seq2) 
             15 GET_ITER             
        >>   16 FOR_ITER                16 (to 35) 
             19 STORE_FAST               2 (y) 
             22 LOAD_FAST                1 (x) 
             25 LOAD_FAST                2 (y) 
             28 BINARY_ADD           
             29 LIST_APPEND              3 
             32 JUMP_ABSOLUTE           16 
        >>   35 JUMP_ABSOLUTE            6 
        >>   38 RETURN_VALUE        

如您所见, for 的字节码listcomp有一个明确FOR_ITER的 over seq2。这个显式FOR_ITER在 listcomp 函数内部,因此对范围的限制仍然适用(例如seq2,作为全局加载)。

事实上,我们可以使用以下方法确认这一点pdb

>>> import pdb
>>> def test(seq1, seq2): pdb.set_trace()
... 
>>> test([1,2,3], [4,5,6])
--Return--
> <stdin>(1)test()->None
(Pdb) [x + y for x in seq1 for y in seq2]
*** NameError: global name 'seq2' is not defined
(Pdb) [x + y for x in non_existent for y in seq2]
*** NameError: name 'non_existent' is not defined

注意NameErroris aboutseq2和 not seq1(作为函数参数传递),并注意将第一个可迭代名称更改为不存在的名称如何更改 the NameError(这意味着在第一种情况下seq1成功传递)。

于 2013-06-25T06:34:43.410 回答
-5

如果您希望为 seq 中的每个元素生成 True 列表,我只是不明白为什么需要执行上述操作,那么为什么不 [True for x in seq] - 我猜您需要分配一个本地在尝试这种事情之前先复制。

于 2013-06-25T06:26:17.140 回答