Python 2 __missing__方法 [英] Python 2 __missing__ method
问题描述
我写了一个非常简单的程序来对字典进行子类化.我想尝试python中的 __ missing __
方法.经过一番研究,我发现在Python 2中,它可以在 defaultdict
中使用.(虽然在python 3中,我们使用collections.UserDict ..)如果找不到密钥,则 __ getitem __
负责调用 __ missing __
方法.
I wrote a very simple program to subclass a dictionary. I wanted to try the __missing__
method in python.
After some research i found out that in Python 2 it's available in defaultdict
. ( In python 3 we use collections.UserDict though..)
The __getitem__
is the on responsible for calling the __missing__
method if the key isn't found.
当我在以下程序中实现 __ getitem __
时,我遇到了关键错误,但是当我在没有它的情况下实现时,我得到了所需的值.
When i implement __getitem__
in the following program i get a key error, but when i implement without it, i get the desired value.
import collections
class DictSubclass(collections.defaultdict):
def __init__(self,dic):
if dic is None:
self.data = None
else:
self.data = dic
def __setitem__(self,key,value):
self.data[key] = value
########################
def __getitem__(self,key):
return self.data[key]
########################
def __missing__(self,key):
self.data[key] = None
dic = {'a':4,'b':10}
d1 = DictSubclass(dic)
d2 = DictSubclass(None)
print d1[2]
我认为我需要实现 __ getitem __
,因为它负责调用 __ missing __
.我了解defaultdict的类定义具有 __ getitem __
方法.但是即使如此,说我想写自己的 __ getitem __
,我该怎么做?
I thought i needed to implement __getitem__
since it's responsible for calling __missing__
. I understand that the class definition of defaultdict has a __getitem__
method. But even so, say i wanted to write my own __getitem__
, how would i do it?
推荐答案
dict
类型将总是尝试调用 __ missing __
. defaultdict
所做的只是提供一个实现.如果您要提供自己的 __ missing __
方法,则不必完全继承 defaultdict
的子类.
The dict
type will always try to call __missing__
. All that defaultdict
does is provide an implementation; if you are providing your own __missing__
method you don't have to subclass defaultdict
at all.
请参见 dict
文档:
d [key]
用键 key 返回 d 的项目.如果 key 不在地图中,则引发KeyError
.
d[key]
Return the item of d with key key. Raises aKeyError
if key is not in the map.
如果dict的子类定义了方法 __ missing __()
并且 key 不存在,则 d [key]
操作将调用该方法以键 key 作为参数.然后, d [key]
操作将返回或引发 __ missing __(key)
调用所返回或引发的内容.没有其他操作或方法调用 __ missing __()
.
If a subclass of dict defines a method __missing__()
and key is not present, the d[key]
operation calls that method with the key key as argument. The d[key]
operation then returns or raises whatever is returned or raised by the __missing__(key)
call. No other operations or methods invoke __missing__()
.
但是,您需要保留默认的 __ getitem __
方法,或者至少调用它.如果您使用自己的版本覆盖 dict.__getitem__
并且不调用基本实现,则永远不会调用 __missing__
.
However, you need to leave the default __getitem__
method in place, or at least call it. If you override dict.__getitem__
with your own version and not call the base implementation, __missing__
is never called.
您可以从自己的实现中调用 __ missing __
:
You could call __missing__
from your own implementation:
def __getitem__(self, key):
if key not in self.data:
return self.__missing__(key)
return self.data[key]
或者您可以调用原始实现:
or you could call the original implementation:
def __getitem__(self, key):
if key not in self.data:
return super(DictSubclass , self).__getitem__(key)
return self.data[key]
在Python 2中,您可以仅继承 UserDict.UserDict
:
In Python 2, you can just subclass UserDict.UserDict
:
from UserDict import UserDict
class DictSubclass(UserDict):
def __missing__(self, key):
self.data[key] = None
这篇关于Python 2 __missing__方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!