pythondictionaryabstract-base-class

What does isinstance with a dictionary and abc.Mapping from collections doing?


The code I'm running is:

>>> from collections import abc
>>> mydict = {'test_key': 'test_value'}
>>> isinstance(mydict, abc.Mapping)
True

I understand what isinstance does, but I'm not sure what abc.Mapping does from collections?

It seems like the line isinstance(mydict, abc.Mapping) is being used to check that mydict is a dictionary?

Wouldn't it just be easier to do isinstance(mydict, dict)?

I did some searching and found related comments in this thread: What is the best (idiomatic) way to check the type of a Python variable?, but I'm still having trouble figuring out why using abc.Mapping is preferable here than just using dict.


Solution

  • collections.abc provides a series of Abstract Base Classes for a container.

    This module provides abstract base classes that can be used to test whether a class provides a particular interface; for example, whether it is hashable or whether it is a mapping.

    They allow you to check if a certain object has a behavior similar to that of the ABC that you are checking without caring for the actual implementation.

    For example, say that you have a function F that does something: according to the type of the argument, you can check if is a instance of list or tuple or dict or etc directly, and do your job, but that limits you to only being able to use those types of collection. If you then make your own class that has a similar behavior to say a list (in some cases you care about) and want to use it with F, you find it doesn't work, then you'll have to modify F to accept your class, but if instead you check against an ABC such modification is unneeded.

    A working example: say that you want a function that returns the elements at an even index from a list, then you can do:

    def even_pos(data):
        if isinstance(data,list):
            return [data[i] for i in range(0,len(data),2)]
        else:
            raise ValueError("only a list")
    

    And use as:

    >>> test = list(range(20))
    >>> even_pos(test)
    [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
    >>>
    

    No problem there, but then you realize that a tuple is the same as a list in what this function concerns so you can add that check to the function too.

    Then your friend tells you he wants to use your function but he is using a collections.deque and then your other friend needs... See the pattern here? All the objects I mention (list, tuple, deque) have in common the same thing, and can be used in the same way by that example function. All that behavior is compressed in the ABC, so instead of isinstance(data,(list,tuple,collections.deque,...) you only need isinstance(data,abc.Sequence) and the function now looks like:

    from collections import abc
    def even_pos(data):
        if isinstance(data,abc.Sequence):
            return [data[i] for i in range(0,len(data),2)]
        else:
            raise ValueError("only a Sequence")
    
    >>> even_pos( list(range(10)) )
    [0, 2, 4, 6, 8]
    >>> even_pos( tuple(range(10)) )
    [0, 2, 4, 6, 8]
    >>> even_pos( range(10) )  # in python 3 range don't return a list, but a range object
    [0, 2, 4, 6, 8]
    >>> even_pos( "asdfghjh" )
    ['a', 'd', 'g', 'j']
    >>> 
    

    Now you don't need to know the actual implementation that is in use, only that it has the behavior that you want.