On 12/22/2010 9:20 AM, kj wrote:
from collections import Mapping
Documented as an *ABSTRACT* base class. ABCs were added in 3.0 and
backparted to 2.7. One can be quite competant in Python completely
ignoring ABCs.
issubclass(dict, Mapping)
True
Yes, dict is a concrete Mapping class. I suppose we could have instead
added a new builtin function 'isconcretetizationof' but is seemed easier
to reuse issubclass for the test. Most people have no problem with that.
dict.__bases__
(<type 'object'>,)
The one and only *CONCRETE* base class. In 3.x, all classes are
subclasses of object, which simplifies the class model a bit.
[issubclass(b, Mapping) for b in dict.__bases__]
[False]
So dict is a subclass of Mapping, even though none of the bases of
dict is either Mapping or a subclass of Mapping. Great.
Right. dict is direct concrete Mapping implementation and not subclassed
from one.
The main reason for introducing ABCs was to make it easier to test
whether an object passed to a function is an instance of a possibly
unknown or yet-to-be-written class in an abstract category, which has a
certain api or interface. The actual usage of an ABC would be more like
this:
from collections import Mapping
def f(m):
if not isinstance(m, Mapping):
raise ValueError('input is not a mapping')
else: return True
f(dict())
# True
f([])
# produces
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
f([])
File "<pyshell#6>", line 3, in f
raise ValueError('input is not a mapping')
ValueError: input is not a mapping
--
Terry Jan Reedy
--
http://mail.python.org/mailman/listinfo/python-list