Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > issubclass(C, Mapping) not behaving as expected

Reply
Thread Tools

issubclass(C, Mapping) not behaving as expected

 
 
anntzer.lee@gmail.com
Guest
Posts: n/a
 
      05-30-2012
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
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      05-31-2012
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java String.replaceAll() not behaving as expected William Krick Java 4 12-18-2008 03:08 PM
<div style="display:inline;"> not behaving as expected Steve Richter ASP .Net 2 05-28-2007 11:48 PM
custom errors not behaving as expected ilyasdhin@googlemail.com ASP .Net 1 04-16-2007 10:32 AM
writeChars not behaving as expected Ken Kafieh Java 7 05-18-2004 11:58 PM
sys._getframe() not behaving as expected Skip Montanaro Python 0 12-01-2003 09:05 PM



Advertisments