Search code examples
pythondefaultdict

Defaultdict and constant_factory vs lambda


The documentation for defaultdict (from collections) provides an example:

A faster and more flexible way to create constant functions is to use itertools.repeat() which can supply any constant value (not just zero):

def constant_factory(value):
    return itertools.repeat(value).next
d = defaultdict(constant_factory('<missing>'))
d.update(name='John', action='ran')

'%(name)s %(action)s to %(object)s' % d
>>>'John ran to <missing>'

So that seems to work, but so does this:

d = defaultdict(lambda: '<missing>')
d.update(name='John', action='ran')

'%(name)s %(action)s to %(object)s' % d
>>>'John ran to <missing>'

This last version seems more compact and cleaner to me. Is there a clear speed or memory or maintainability (or any other) reason to define constant_factory rather than using a lambda function? I'm using this in some code that will be used (and maintained) by others, so I want to make sure I'm not just playing to my own preferences.


Solution

  • itertools.repeat(value).next is much faster than a lambda function. This is because a lambda function executes python bytecode, while itertools primitives are implemented in C.