5

如何为 defaultdict 获取可调用工厂以允许对其进行理解?我认为这可能是不可能的,但我想不出一个很好的理由?

>>> def foo(*args):
...     # TODO
...
>>> from collections import defaultdict
>>> thing = foo(defaultdict, int)
>>> d = thing((i, i*i) for i in range(3))
>>> d[2]
# should return 4
>>> d[-1]
# should return 0
4

2 回答 2

9

Any arguments to defaultdict after the default_factory are treated just like arguments to dict:

>>> defaultdict(int, [(i, i*i) for i in range(5)])
defaultdict(<type 'int'>, {0: 0, 1: 1, 2: 4, 3: 9, 4: 16})

Just pass the comprehension to defaultdict and let it do the work:

def defaultdict_factory_factory(default_factory):
    def defaultdict_factory(*args, **kwargs):
        return defaultdict(default_factory, *args, **kwargs)
    return defaultdict_factory

Or use functools.partial:

def defaultdict_factory_factory(default_factory):
    return partial(defaultdict, default_factory)
于 2014-01-14T00:39:54.030 回答
5

你只是在寻找defaultdict.update吗?

>>> from collections import defaultdict
>>> thing = defaultdict(int)
>>> thing.update((i, i*i) for i in range(3))
>>> thing
defaultdict(<type 'int'>, {0: 0, 1: 1, 2: 4})

你可以把它放到一个函数中。

>>> def initdefaultdict(type_, *args, **kwargs):
...     d = defaultdict(type_)
...     d.update(*args, **kwargs)
...     return d
... 
>>> thing = initdefaultdict(int, ((i, i+10) for i in range(3)))
>>> thing
defaultdict(<type 'int'>, {0: 10, 1: 11, 2: 12})
>>> thing[3]
0

或者为了满足您的原始要求,返回一个函数:

>>> def defaultdictinitfactory(type_): # this is your "foo"
...     def createupdate(*args, **kwargs):
...             d = defaultdict(type_)
...             d.update(*args, **kwargs)
...             return d
...     return createupdate
... 
>>> f = defaultdictinitfactory(int) # f is your "thing"
>>> d = f((i, i*i) for i in range(3))
>>> d
defaultdict(<type 'int'>, {0: 0, 1: 1, 2: 4})
>>> 
于 2014-01-14T00:22:11.130 回答