issubclass(C, Mapping) not behaving as expected

A

anntzer.lee

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
 
S

Steven D'Aprano

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top