(I am using python 2.7) The python documentation indicates that you can pass a mapping to the dict builtin and it will copy that mapping into the new dict:
http://docs.python.org/library/stdtypes.html#mapping-types-dict
I have a class that implements the Mapping ABC, but it fails:
import collections
class Mapping(object):def __init__(self, dict={}): self.dict=dictdef __iter__(self): return iter(self.dict)def __iter__(self): return iter(self.dict)def __len__(self): return len(self.dict)def __contains__(self, value): return value in self.dictdef __getitem__(self, name): return self.dict[name]m=Mapping({5:5})
dict(m)
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# TypeError: cannot convert dictionary update sequence element #0 to a sequence
collections.Mapping.register(Mapping)
dict(m)
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# TypeError: cannot convert dictionary update sequence element #0 to a sequence
However, if my class subclasses collections.Mapping then it works fine:
import collections
class Mapping(collections.Mapping):def __init__(self, dict={}): self.dict=dictdef __iter__(self): return iter(self.dict)def __iter__(self): return iter(self.dict)def __len__(self): return len(self.dict)def __contains__(self, value): return value in self.dictdef __getitem__(self, name): return self.dict[name]m=Mapping({5:5})
dict(m)
# {5: 5}
I thought the whole point of the ABCs was to allow registration to work the same as subclassing (for isinstance and issubclass anyway). So what's up here?