issubclass(C, Mapping) not behaving as expected

Discussion in 'Python' started by anntzer.lee@gmail.com, May 30, 2012.

  1. Guest

    from collections import *
    class C(object):
    def __iter__(self): pass
    def __contains__(self, i): pass
    def __len__(self): pass
    def __getitem__(self, i): pass
    issubclass(C, Mapping) => False
    [issubclass(C, cls) for cls in Mapping.__mro__] => [False, True, True, True, True]
    i.e. C does implement Sized, Iterable and Container.

    I would have expected that just as issubclass(C, Sized) checks for the presence of a "__len__" method, issubclass(C, Mapping) would check for the presence of the three methods required by each immediate superclass?

    Antony
    , May 30, 2012
    #1
    1. Advertising

  2. On Wed, 30 May 2012 00:55:00 -0700, anntzer.lee wrote:

    > from collections import *
    > class C(object):
    > def __iter__(self): pass
    > def __contains__(self, i): pass
    > def __len__(self): pass
    > def __getitem__(self, i): pass
    >
    > issubclass(C, Mapping) => False
    > [issubclass(C, cls) for cls in Mapping.__mro__] => [False, True, True,
    > True, True] i.e. C does implement Sized, Iterable and Container.
    >
    > I would have expected that just as issubclass(C, Sized) checks for the
    > presence of a "__len__" method, issubclass(C, Mapping) would check for
    > the presence of the three methods required by each immediate superclass?


    What makes you think it doesn't? Perhaps it does, but there are other
    requirements for Mapping that are not being satisfied.

    I must admit that the documentation for collections and in particular for
    Mapping and MutableMapping are a bit unclear to me. But have you tried
    registering your class as a Mapping explicitly?


    --
    Steven
    Steven D'Aprano, May 31, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken Kafieh
    Replies:
    7
    Views:
    2,471
    Roedy Green
    May 19, 2004
  2. William Krick
    Replies:
    4
    Views:
    77,820
    rt19983
    Dec 18, 2008
  3. Skip Montanaro

    sys._getframe() not behaving as expected

    Skip Montanaro, Dec 1, 2003, in forum: Python
    Replies:
    0
    Views:
    345
    Skip Montanaro
    Dec 1, 2003
  4. Replies:
    1
    Views:
    335
    David
    Apr 16, 2007
  5. kj

    issubclass(dict, Mapping)

    kj, Dec 22, 2010, in forum: Python
    Replies:
    10
    Views:
    525
Loading...

Share This Page