Python Forum
how to test if a value is or is like a dictionary - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: how to test if a value is or is like a dictionary (/thread-15451.html)



how to test if a value is or is like a dictionary - Skaperen - Jan-17-2019

i have a function that needs to test if a value given to it in an argument is a dictionary or not. doing isinstance(thevalue,dict) is not good enough when the value comes from os.environ. but i would want the test to be positive for os.environ and anything like it as well as any genuine dictionary. what kind of test would work?


RE: how to test if a value is or is like a dictionary - ichabod801 - Jan-17-2019

collections.abc.MutableMapping

Output:
>>> import os >>> import collections.abc >>> isinstance(os.environ, collections.abc.MutableMapping) True >>> isinstance({}, collections.abc.MutableMapping) True