当Foo.__contains__
没有定义:
a['bar']
电话Foo.__getitem__
,其执行
self[key] = data
这就要求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)
由于Foo.__contains__
未定义,
if key not in self:
为真。因此,密钥已正确添加到self.__root
和self.__map
。
当Foo.__contains__
定义,
if key not in self:
如果假。所以密钥没有正确添加到self.__root
和self.__map
。 Foo.__contains__
有效的傻瓜OrderedDict.__setitem__
认为'bar'
密钥已被添加。
我发现它有助于用下面的代码打(添加在__setitem__
打印报表__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']
注意,您可以通过使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
其产生
a = Foo()
print a['bar']
# barbar
print a.keys()
# ['bar']
即使定义
__contains__
。
我正在阅读[来源](http://hg.python.org/cpython/file/2.7/Lib/collections.py),我仍然很难找出这一个...... – mgilson 2013-03-09 22:36:00
我这样做,我想你的问题在哪里:看看'__setitem__'和'__iter__'。 – 2013-03-09 22:38:43
@ A.Rodas - 是的,那就是我在找的地方。也许我太累了,但我很难保持所有的逻辑。 – mgilson 2013-03-09 22:43:18