在函数中调试:无法列出压缩对象?

Debug in a function: cannot list zipped objects?

我正在尝试像这样调试函数 one_away

import pdb


def is_one_away(first: str, other: str) -> bool:

    skip_diff = {
        -1: lambda i: (i, i + 1),
        1: lambda i: (i + 1, i),
        0: lambda i: (i + 1, i + 1)
    }
    try:
        skip = skip_diff[len(first) - len(other)]
    except KeyError:
        return False
    pdb.set_trace()
    for i, (l1, l2) in enumerate(zip(first, other)):
        if l1 != l2:
            i -= 1
            break

我这样称呼它:

import one_away
one_away.is_one_away('pale', 'kale')

当运行pdb.set_trace()时,我想看看zip(first ,other)的结果。所以我写:

(Pdb) >? list(zip(first, other))
*** Error in argument: '(zip(first, other))'

但如果在 Python 控制台中,它会起作用:

>>>list(zip('pale', 'kale'))
[('p', 'k'), ('a', 'a'), ('l', 'l'), ('e', 'e')]

为什么?

list是调试中的内置命令 shell:

(Pdb) help list
l(ist) [first [,last] | .]

        List source code for the current file.  Without arguments,
        list 11 lines around the current line or continue the previous
        listing.  With . as argument, list 11 lines around the current
        line.  With one argument, list 11 lines starting at that line.
        With two arguments, list the given range; if the second
        argument is less than the first, it is a count.

        The current line in the current frame is indicated by "->".
        If an exception is being debugged, the line where the
        exception was originally raised or propagated is indicated by
        ">>", if it differs from the current line.

所以当你输入

(Pdb) list(zip(first, other))

它试图将其解释为传递给 pdb 的命令。相反,您想在 python:

中执行(或 print)一个表达式
(Pdb) list(zip(first, other))
*** Error in argument: '(zip(first, other))'
(Pdb) !list(zip(first, other))
[('p', 'k'), ('a', 'a'), ('l', 'l'), ('e', 'e')]
(Pdb) p list(zip(first, other))
[('p', 'k'), ('a', 'a'), ('l', 'l'), ('e', 'e')]