'How to check an object has the type 'dict_items'?

In Python 3, I need to test whether my variable has the type 'dict_items', so I tried something like that :

>>> d={'a':1,'b':2}
>>> d.items()
dict_items([('a', 1), ('b', 2)])
>>> isinstance(d.items(),dict_items)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'dict_items' is not defined

But dict_items is not a known type. it is not defined in types module neither. How can I test an object has the type dict_items (without consuming data) ?



Solution 1:[1]

I prefer this approach

d={'a':1,'b':2}
d.items()

assert type(d.items()).__name__ == 'dict_items', 'Not dict_items!!!!'
assert d.items().__class__.__name__ == 'dict_items', 'Not dict_items!!!!'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 NotoriousPyro