I need to test if objects read in from a file (and eval
ed) with ConfigParser
are mappings.
Not entirely sure about the terminology here, but let me explain. Given that my object is called O
it must support being used in the following manner:
def tester(**kwargs):
print kwargs
tester(**O)
If O
was not supporting the ** this would result in a TypeError, e.g. TypeError: test() argument after ** must be a mapping, not tuple
.
It's a really simple scenario, but I need to know that O
will work before using it and I need to be absolutely certain it won't fail. If I was testing O
for being an iterable, I would use something like:
try:
iter(O)
except:
O = tuple()
As discussed in In Python, how do I determine if an object is iterable?
I can't find any parallel for mappings. As discussed in the same answer above, using isinstance
and collections
isn't an good solution.
So do I have to make my tester-function above (without the print) as my own mapping-test when loading the objects like
try:
tester(**O)
except TypeError:
O = {}
or does python have a built in way to test this like there is for iterables? It seems there should be one.
Edit
Actually the linked answer above never spoke against the isinstance method, should have read it better...