为什么重写__contains__会破坏OrderedDict.keys?

mgi*_*son 6 python python-2.7

我正在继承OrderedDict(Cpython,2.7.3)来表示数据文件. __getitem__从数据文件中提取一个字段并将其设置在当前实例上,类似于我在下面发布的代码.现在我想覆盖__contains__返回,True如果字段在字典中或磁盘上的文件中,因为它可以以任何方式读取.然而,这似乎打破OrderedDict了检查它的钥匙的能力.

from collections import OrderedDict

dictclass = OrderedDict

class Foo(dictclass):
    def __getitem__(self,key):
        try:
            return dictclass.__getitem__(self,key)
        except KeyError:
            pass

        data = key*2
        self[key] = data
        return data

    def __contains__(self,whatever):
        return dictclass.__contains__(self,whatever) or 'bar' in whatever

a = Foo()
print a['bar']
print a.keys()
Run Code Online (Sandbox Code Playgroud)

如果您运行上面的代码,您将获得此输出:

barbar
[]
Run Code Online (Sandbox Code Playgroud)

请注意,如果您更改dictclass = dict上面的代码,它似乎仍然有效(给出以下输出).

barbar
['bar']
Run Code Online (Sandbox Code Playgroud)

我做错了什么吗?

unu*_*tbu 6

什么时候Foo.__contains__没有定义:

a['bar']
Run Code Online (Sandbox Code Playgroud)

调用Foo.__getitem__,执行

    self[key] = data
Run Code Online (Sandbox Code Playgroud)

这个调用OrderedDict.__setitem__,以这种方式定义:

def __setitem__(self, key, value, PREV=0, NEXT=1, dict_setitem=dict.__setitem__):
    'od.__setitem__(i, y) <==> od[i]=y'
    # Setting a new item creates a new link at the end of the linked list,
    # and the inherited dictionary is updated with the new key/value pair.
    if key not in self:
        root = self.__root
        last = root[PREV]
        last[NEXT] = root[PREV] = self.__map[key] = [last, root, key]
    dict_setitem(self, key, value)
Run Code Online (Sandbox Code Playgroud)

既然Foo.__contains__没有定义,

    if key not in self:
Run Code Online (Sandbox Code Playgroud)

是真的.所以关键是正确添加到self.__rootself.__map.

什么时候Foo.__contains__定义,

    if key not in self:
Run Code Online (Sandbox Code Playgroud)

如果错.所以密钥没有正确添加到self.__rootself.__map. Foo.__contains__有效的傻瓜OrderedDict.__setitem__认为'bar'已经添加了密钥.


我发现使用以下代码(在__setitem__和中添加print语句__iter__)很有帮助:

from collections import OrderedDict

dictclass = OrderedDict

class Foo(dictclass):
    def __getitem__(self,key):
        try:
            return dictclass.__getitem__(self,key)
        except KeyError:
            pass

        data = key*2
        self[key] = data
        return data

    def __contains__(self,whatever):
        print('contains: {}'.format(whatever))
        return dictclass.__contains__(self,whatever) or 'bar' in whatever

    def __setitem__(self, key, value, PREV=0, NEXT=1, dict_setitem=dict.__setitem__):
        'od.__setitem__(i, y) <==> od[i]=y'
        # Setting a new item creates a new link at the end of the linked list,
        # and the inherited dictionary is updated with the new key/value pair.
        print('key not in self: {}'.format(key not in self))
        if key not in self:
            root = self._OrderedDict__root
            last = root[PREV]
            last[NEXT] = root[PREV] = self._OrderedDict__map[key] = [last, root, key]
        dict_setitem(self, key, value)

    def __iter__(self):
        'od.__iter__() <==> iter(od)'
        # Traverse the linked list in order.
        NEXT, KEY = 1, 2

        root = self._OrderedDict__root
        curr = root[NEXT]
        print('curr: {}'.format(curr))
        print('root: {}'.format(root)) 
        print('curr is not root: {}'.format(curr is not root))

        while curr is not root:
            yield curr[KEY]
            curr = curr[NEXT]

a = Foo()
print a['bar']
# barbar

print a.keys()
# ['bar']
Run Code Online (Sandbox Code Playgroud)

请注意,您可以通过创建Foo子类collections.MutableMapping并将其大部分行为委托给OrderedDict属性来避免此问题:

import collections
dictclass = collections.OrderedDict

class Foo(collections.MutableMapping):
    def __init__(self, *args, **kwargs):
        self._data = dictclass(*args, **kwargs)
    def __setitem__(self, key, value):
        self._data[key] = value
    def __delitem__(self, key):
        del self._data[key]
    def __iter__(self):
        return iter(self._data)
    def __len__(self):
        return len(self._data)

    def __getitem__(self,key):
        try:
            return self._data[key]
        except KeyError:
            pass

        data = key*2
        self[key] = data
        return data

    def __contains__(self,whatever):
        return dictclass.__contains__(self,whatever) or 'bar' in whatever
Run Code Online (Sandbox Code Playgroud)

产量

a = Foo()
print a['bar']
# barbar

print a.keys()
# ['bar']
Run Code Online (Sandbox Code Playgroud)

即使有__contains__定义.