confused confused - 1 year ago 87
Python Question

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

I'm learning pdb and I can print, or pp a list of objects but how can I print the type of key for each object? I can see it with pp, it looks like a byte array but I'd like to know the type. I suppose I could just print debug this but I'm curious if there's a smarter way to do it when using the debugger.

Answer Source

Because you wrote

type of key

I'm assuming that you mean a dictionary. But you do also talk about a "list of objects", which could also mean

  • a list of any kind of object
  • a list of dictionaries

But I'll show you two options:

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()

Now when pdb fires up:

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

That will give you the keys and types of the keys.

Here's the types and values from the list:

(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'>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download