Fast constant functions for Py2.5's defaultdict()

Raymond Hettinger python at rcn.com
Wed Feb 14 03:11:36 EST 2007


On Feb 13, 5:09 pm, Giovanni Bajo <n... at ask.me> wrote:
> > The itertools.repeat(const).next approach wins on speed and
> > flexibility.
>
> But it's the most unreadable too.

Not really.  It's unusual but plenty readable (no surprise that
repeat(0) repeatedly gives you zero).  I think it more surprising that
int() with no arguments gives you a zero.

> I'm surprised that defaultdict(int) is
> slower than the lambda one though. What's the reason?

All that comes to mind is that int() has to call
PyArg_ParseTupleAndKeywords() while the lambda is unburdened by
argument passing.


Raymond





More information about the Python-list mailing list