How to distinguish between a sequence and a mapping
Question: I would like to perform an operation on an argument based on the fact that it
might be a map-like object or a sequence-like object. I understand that no
strategy is going to be 100% reliable for type-like checking, but I'm looking
for a robust solution.

Based on this [answer](http://stackoverflow.com/questions/305359/correct-way-
to-detect-sequence-parameter/425567#425567), I know how to determine whether
something is a sequence and I can do this check after checking if the object
is a map.

    
    
    def ismap(arg):
       # How to implement this?
    
    def isseq(arg):
       return hasattr(arg,"__iter__")
    
    def operation(arg):
       if ismap(arg):
          # Do something with a dict-like object
       elif isseq(arg):
          # Do something with a sequence-like object
       else:
          # Do something else
    

Because a sequence can be seen as a map where keys are integers, should I just
try to find a key that is not an integer? Or maybe I could look at the string
representation? or...?

**UPDATE**

I selected SilentGhost's answer because it looks like the most "correct" one,
but for my needs, here is the solution I ended up implementing:

    
    
    if hasattr(arg, 'keys') and hasattr(arg, '__getitem__'):
       # Do something with a map
    elif hasattr(arg, '__iter__'):
       # Do something with a sequence/iterable
    else:
       # Do something else
    

Essentially, I don't want to rely on an ABC because there are many custom
classes that behave like sequences and dictionary but that still do not extend
the python collections ABCs (see @Manoj comment). I thought the keys attribute
(mentioned by someone who removed his/her answer) was a good enough check for
mappings.

Classes extending the Sequence and Mapping ABCs will work with this solution
as well.


Answer: 
    >>> from collections import Mapping, Sequence
    >>> isinstance('ac', Sequence)
    True
    >>> isinstance('ac', Mapping)
    False
    >>> isinstance({3:42}, Mapping)
    True
    >>> isinstance({3:42}, Sequence)
    False
    

[`collections` abstract base classes
(ABCs)](http://docs.python.org/library/collections.html#abcs-abstract-base-
classes)

