4

Use of isinstance() changed the class type of dict Why is this happening? I know using builtins would prevent but I want to understand better why this is happening.

250     def printPretty(records,num,title='Summary:'):
251         import pdb; pdb.set_trace()
252         if isinstance(records, list):
253             print ("\n{}\n{}".format(title.center(120),"="*120))
254             table = list()
255             for i in records:
...
263         elif isinstance(records, dict):
264  ->         for key in records:
265                 if isinstance(records[key], Param):
266                     for i in records[key]:
267                         print (i)
268                 print ("")
269     
(Pdb) type(records)
<class 'dict'>
(Pdb) type(dict)
<class 'type'><b>
Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
user113411
  • 69
  • 1

1 Answers1

3

I think your confusion lies in the fact that type(dict) != dict. Lets discard your example entirely except for the last two lines, which I will use interactive python to present.

>>> type(dict)
<type 'type'>
>>> type(dict())
<type 'dict'>

This is because dict is not a dictionary, but the type of dictionaries. dict() or {} (or {1:2, ...}) are instances of dictionaries. These instances have type of dict, and satisfy isinstance(___, dict).

Cireo
  • 4,197
  • 1
  • 19
  • 24