如何在 python pdb 中打印列表的键类型

how to print type of keys for a list in python pdb

我正在学习 pdb,我可以打印或 pp 对象列表,但如何打印每个对象的键类型?我可以用 pp 看到它,它看起来像一个字节数组,但我想知道类型。我想我可以只打印 debug this 但我很好奇在使用调试器时是否有更聪明的方法来做到这一点。

因为你写了

type of key

我假设你指的是字典。但是你也讲一个"list of objects",也可能是

  • 任何类型对象的列表
  • 词典列表

但我会告诉你两个选项:

mydict = {b'some bytes': 42,
          'a string!': 'fnord',
          (1,2,3): 'A tuple! (is that two-pull or tuh-ple?)',
          19: 'Just an int',
          }

list_of_things = [b'some bytes', 'a string!', (1,2,3), 19, ['a', 'b', 'c']]

import pdb; pdb.set_trace()

现在当 pdb 启动时:

(Pdb) for _ in mydict: print('{} {}'.format(_, type(_)))
19 <type 'int'>
some bytes <type 'str'>
a string! <type 'str'>
(1, 2, 3) <type 'tuple'>

这将为您提供密钥和密钥类型。

这是列表中的类型和值:

(Pdb) for _ in list_of_things: print('{} {}'.format(_, type(_)))
some bytes <type 'str'>
a string! <type 'str'>
(1, 2, 3) <type 'tuple'>
19 <type 'int'>
['a', 'b', 'c'] <type 'list'>