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)
012 के बाद सेपरिभाषित नहीं किया गया है,
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
मैं ऐसा कर रहा हूं और मुझे लगता है कि आपकी समस्या कहां है: '__setitem__' और' __iter__' पर एक नज़र डालें। –
@ एरोडास - हाँ, यही वह जगह है जहां मैं देख रहा था। हो सकता है कि मैं बहुत थक गया हूं, लेकिन मुझे सभी तर्क सीधे रखने में मुश्किल हो रही थी। – mgilson