Отладка в функции: нельзя перечислить заархивированные объекты? - PullRequest
0 голосов
/ 01 ноября 2018

Я пытаюсь отладить функцию 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')]

Почему?

1 Ответ

0 голосов
/ 01 ноября 2018

list - это встроенная команда в оболочке отладки:

(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. Вместо этого вы хотите выполнить (или p rint) выражение в python:

(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')]
...