0

I can't understand following execution. I expected different results.

>>> f = {'ms':'ma'}
>>> isinstance(f['ms'], type(str))
False

>>> isinstance(f['ms'], type(dict))
False

>>> type(f['ms'])
<class 'str'>
waltexwq
  • 53
  • 2
  • 10
  • 5
    `type(str)` returns `type`, so you are checking if `f['ms']` is an instance of `type`, not an instance of `str`. If you want to check if something is a string, use `isinstance(f['ms'], str)`. – khelwood Nov 15 '18 at 11:42

2 Answers2

4

type(str) and type(dict) each return type, so you are checking if your objects are instances of type, which they are not.

If you want to check if something is a string, use

isinstance(f['ms'], str)

not

isinstance(f['ms'], type(str))

And if you want to test if something is a dict, you can use

isinstance(f['ms'], dict)

not

isinstance(f['ms'], type(dict))
khelwood
  • 55,782
  • 14
  • 81
  • 108
1

I think you just want this:

>>> f = {'ms':'ma'}
>>> isinstance(f['ms'], str)
True

You don't need type(str)

richflow
  • 1,902
  • 3
  • 14
  • 21