Dictionary from list?

Greg Chapman glchapman at earthlink.net
Sun Oct 28 12:39:05 EST 2001


On Sat, 27 Oct 2001 13:16:43 -0400, "Tim Peters" <tim.one at home.com> wrote:

>"Sequence" is a slippery word.  dictionary() continues to accept a mapping
>object too.  Of course, "mapping object" is also a slippery phrase.

On the subject of slippery mappings and the dictionary constructor, consider a
subclass of dictionary which overrides __getitem__ to transform the value stored
in the (inherited) dictionary structure to the "real" value stored in the
(logical) mapping defined by the dictionary subclass.  (For example: a
dictionary subclass which supports a defined order of iteration by using nodes
in a linked list to store the keys and values, and then storing the keys and
nodes in the dictionary structure).  In the 2.2b1, if an instance of such a
subclass is passed to the constructor (or to the update method) of a normal
dictionary, the overridden __getitem__ is ignored because of the PyDict_Check
near the top of PyDict_Merge.  I'd like to suggest that that check be changed to
PyDict_CheckExact (which apparently does not exist yet, but would be analogous
to PyString_CheckExact).  This would shunt dictionary subclasses into the
generic mapping branch of PyDict_Merge, which would allow an overridden
__getitem__ to work.

Alternatively, the PyDict_Check could be supplemented by a check to see if
__getitem__ has been overridden (and if so, using the generic code).  However,
this would not help a subclass which transforms its keys in some way (so that
PyMapping_Keys returns a different set of keys than that stored in the
dictionary structure).  (I suppose the check could be extended to look for an
overridden keys method.)

---
Greg Chapman




More information about the Python-list mailing list