List comprehension scope error from Python debugger

后端 未结 2 1161
鱼传尺愫
鱼传尺愫 2020-12-03 02:41

In debugging my code, I want to use a list comprehension. However, it seems I cannot evaluate a list comprehension from the debugger when I\'m inside a function.

I a

2条回答
  •  -上瘾入骨i
    2020-12-03 02:56

    In Python 3, you have to use the interact command in pdb before you can access any non-global variables due to a change in the way comprehensions are implemented.

    >>> def foo(): [][0]
    ... 
    >>> foo()
    Traceback (most recent call last):
      File "", line 1, in 
      File "", line 1, in foo
    IndexError: list index out of range
    >>> import pdb;pdb.pm()
    > (1)foo()
    (Pdb) x = 4
    (Pdb) [x for _ in range(2)]
    *** NameError: name 'x' is not defined
    (Pdb) interact
    *interactive*
    >>> [x for _ in range(2)]
    [4, 4]
    >>> 
    

提交回复
热议问题