Python Forum
how to test if a value is or is like a dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to test if a value is or is like a dictionary
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
collections.abc.MutableMapping

Output:
>>> import os >>> import collections.abc >>> isinstance(os.environ, collections.abc.MutableMapping) True >>> isinstance({}, collections.abc.MutableMapping) True
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to test and import a model form computer to test accuracy using Sklearn library Anldra12 6 3,068 Jul-03-2021, 10:07 AM
Last Post: Anldra12
  Using Dictionary to Test Evenness of Distribution Generated by Randint Function new_coder_231013 6 3,199 Feb-23-2021, 01:29 PM
Last Post: new_coder_231013
  How to write test cases for a init function by Unit test in python? binhduonggttn 2 3,062 Feb-24-2020, 12:06 PM
Last Post: Larz60+
  How to write test cases by Unit test for database configuration file? binhduonggttn 0 2,511 Feb-18-2020, 08:03 AM
Last Post: binhduonggttn

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020